Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调整a<;帆布>;元素_Javascript_Html_Css_Canvas - Fatal编程技术网

Javascript 调整a<;帆布>;元素

Javascript 调整a<;帆布>;元素,javascript,html,css,canvas,Javascript,Html,Css,Canvas,我有下面的画布。我试图使用width和height属性使它变小,但它不起作用。这是我的代码: 身体{ 边际:0px; 填充:0px; } //设置变量 var a=document.getElementById('canvas'), c=a.getContext('2d'), w=a.width=window.innerWidth, h=a.height=window.innerHeight, 面积=w*h, 粒子数=300, 动画 var粒子=[]; //创建粒子 函数粒子(一){ 这个i

我有下面的画布。我试图使用
width
height
属性使它变小,但它不起作用。这是我的代码:


身体{
边际:0px;
填充:0px;
}
//设置变量
var a=document.getElementById('canvas'),
c=a.getContext('2d'),
w=a.width=window.innerWidth,
h=a.height=window.innerHeight,
面积=w*h,
粒子数=300,
动画
var粒子=[];
//创建粒子
函数粒子(一){
这个id=i;
this.hue=rand(50,0,1);
this.active=false;
}
Particle.prototype.build=函数(){
该点x=w/2;
这个y=h/2;
r=rand(7,2,1);
this.vx=Math.random()*10-5;
this.vy=Math.random()*10-5;
这个重力=0.01;
this.opacity=Math.random()+.5;
this.active=true;
c、 beginPath();
c、 弧(this.x,this.y,this.r,0,2*Math.PI,false);
c、 fillStyle=“hsla(“+this.hue+”,100%,50%,1)”;
c、 填充();
};
Particle.prototype.draw=函数(){
this.active=true;
this.x+=this.vx;
this.y+=this.vy;
this.vy+=this.gravity;
这个色相=0.5;
this.r=Math.abs(this.r-.05);
c、 beginPath();
c、 弧(this.x,this.y,this.r,0,2*Math.PI,false);
c、 fillStyle=“hsla(“+this.hue+”,100%,50%,1)”;
c、 填充();
//重置粒子

如果(this.r,请在此处多加注意:

var a = document.getElementById('canvas'),
    c = a.getContext('2d'),
    w = a.width = window.innerWidth,
    h = a.height = window.innerHeight,
    area = w * h,
如您所见,
w
是窗口的宽度,
h
是窗口的高度。请尝试使用自定义值编辑
w
h
变量,并告诉我它是否有效;)

编辑:

只需在脚本标记中用以下内容替换
h
w

a.style.width = '500px'; //random value, insert custom here
a.style.height = '500px';

请注意这里:

var a = document.getElementById('canvas'),
    c = a.getContext('2d'),
    w = a.width = window.innerWidth,
    h = a.height = window.innerHeight,
    area = w * h,
如您所见,
w
是窗口的宽度,
h
是窗口的高度。请尝试使用自定义值编辑
w
h
变量,并告诉我它是否有效;)

编辑:

只需在脚本标记中用以下内容替换
h
w

a.style.width = '500px'; //random value, insert custom here
a.style.height = '500px';

谢谢你的帮助!谢谢你的帮助!