Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.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/8/visual-studio-code/3.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 如何在画布上画大量的点(有规则的图案)?_Javascript_Algorithm_Canvas_Graphics - Fatal编程技术网

Javascript 如何在画布上画大量的点(有规则的图案)?

Javascript 如何在画布上画大量的点(有规则的图案)?,javascript,algorithm,canvas,graphics,Javascript,Algorithm,Canvas,Graphics,这些点的排列方式使它们之间有规则的距离 它们在(x%4==0)和(y%4==0)处绘制,它们需要时间以暴力方式绘制: for (var x = 0; x < width; x+=4) { for (var y = 0; y < height; y+=4) { draw(x, y, 1, 1); } } for(变量x=0;x

这些点的排列方式使它们之间有规则的距离

它们在(x%4==0)和(y%4==0)处绘制,它们需要时间以暴力方式绘制:

for (var x = 0; x < width; x+=4) {
    for (var y = 0; y < height; y+=4) {
        draw(x, y, 1, 1);
    }
}
for(变量x=0;x
如何以更好的方式做这件事

您可以先创建一个屏幕外画布,在其中画一个点,然后将该画布用作
createPattern()
的图像源。将图案设置为
fillStyle
并填充

var ctx=c.getContext(“2d”);
var pattern=ctx.createPattern(createPatternImage(),“repeat”);
ctx.fillStyle=图案;
ctx.fillRect(0,0,c.宽度,c.高度);
函数createPatternImage(){
var ctx=document.createElement(“canvas”).getContext(“2d”);
ctx.canvas.width=ctx.canvas.height=4;//=图案底部的大小
ctx.fillStyle=“#fff”;
ctx.fillRect(0,0,1,1);
return ctx.canvas;//画布可以用作图像源
}
您可以先创建一个屏幕外画布,在其中画一个点,然后将该画布用作
createPattern()
的图像源。将图案设置为
fillStyle
并填充

var ctx=c.getContext(“2d”);
var pattern=ctx.createPattern(createPatternImage(),“repeat”);
ctx.fillStyle=图案;
ctx.fillRect(0,0,c.宽度,c.高度);
函数createPatternImage(){
var ctx=document.createElement(“canvas”).getContext(“2d”);
ctx.canvas.width=ctx.canvas.height=4;//=图案底部的大小
ctx.fillStyle=“#fff”;
ctx.fillRect(0,0,1,1);
return ctx.canvas;//画布可以用作图像源
}
draw()做什么?您可以使用图案(即
createPattern()
)。
绘制(x、y、宽度、高度)只做canvas.getContext(“2d”).fillRect(x,y,宽度,高度)draw()做什么?您可以使用图案(即
createPattern()
)。
绘制(x、y、宽度、高度)只做canvas.getContext(“2d”).fillRect(x,y,宽度,高度)