简体   繁体   English

HTML5 Canvas JavaScript Crosshair Grid

[英]HTML5 Canvas JavaScript Crosshair Grid

I wanted to make a crosshair grid (every 10px). 我想做一个十字准线网格(每10px)。 I had many problems with it. 我遇到了很多问题。 Can it be done in easier way than 3x For loop? 它可以比3x For循环更容易地完成吗? http://jsfiddle.net/TnnRp/1/ http://jsfiddle.net/TnnRp/1/

var canvas = document.getElementById('grid');
var context = canvas.getContext('2d');
// grid
var width = canvas.width;
var height = canvas.height;
var p = 10;
var h = 10;
for (var i = 10; i <= width - 5; i += 10) {
    for (var e = 10; e <= height - 5; e += 10) {
        context.moveTo(h + 0.5, e - 1);
        context.lineTo(h + 0.5, e + 2);
    }
    h += 10;
    for (var f = 10; f <= width - 5; f += 10) {
        context.moveTo(f - 1, p + 0.5);
        context.lineTo(f + 2, p + 0.5);
    }
    p += 10;
}
context.stroke();

You can always reduce it to two loops and there are two ways with that as well. 你总是可以将它减少到两个循环,并且有两种方法。 But before: I agree with markE - your code is just fine as it is. 但之前:我同意markE - 你的代码就好了。

My version here is to reduce loops and show one way to optimize its speed: 我的版本是减少循环并显示一种优化其速度的方法:

//pre-translate to force anti-alias
context.translate(0.5, 0.5);

Now we draw just one single cross-hair: 现在我们只画一个单一的十字准线:

var cc = 1; //cross-hair size

context.moveTo(p / 2, h / 2 - cc);
context.lineTo(p / 2, h / 2 + cc);
context.moveTo(p / 2 - cc, h / 2);
context.lineTo(p / 2 + cc, h / 2);

context.stroke();

And now we "blit" our hearts out, first horizontally: 现在我们首先横向地“哄骗”我们的心脏:

//replicate drawn cross-hair = faast.
for (i = 0; i < width - p; i += p) {
    if (i > 0) p *= 2;
    context.drawImage(canvas, 0, 0, p, h, p, 0, p,h);
}

And now we replicate that line vertically: 现在我们垂直复制该行:

for(i = 0; i < height; i+=h) {
    if (i > 0) h *= 2;
    context.drawImage(canvas, 0, 0, width, h, 0, h, width, h);
}

Notice that we are not just copying one line, but when we have draw one replicate, we duplicate those two, then we skip four and copy the four etc. 请注意,我们不只是复制一行,但是当我们绘制一个复制时,我们复制这两个,然后我们跳过四个并复制四个等。

This method is super-fast and is the way the browser (or rather the system function the browser uses) also replicate patterns (but with internal compiled code). 这种方法超快,浏览器(或浏览器使用的系统功能)也可以复制模式(但内部编译代码)。 You could also have used the first cross-hair to set a pattern on an off-screen canvas and filled the canvas with that which could be a notch faster. 您也可以使用第一个十字准线在屏幕外的画布上设置图案,并在画布上填充可能更快的凹槽。

Updated fiddle 更新了小提琴

With Ken's help. 有了Ken的帮助。 Working jsFiddle 工作jsFiddle

var canvas = document.getElementById('grid');
var context = canvas.getContext('2d');

var width = canvas.width,
    height = canvas.height;
context.moveTo(10.5, 10 - 1);
context.lineTo(10.5, 10 + 2);
context.moveTo(10.5 -1, 10.5);
context.lineTo(10.5 +2, 10.5);
context.stroke();
var h=10,
    p=10;

for (i = 0; i < width; i += p) {
    p *= 2;
    context.drawImage(canvas, p, 0);
}
for(i = 0; i < height; i+=h) {
    h *= 2;
    context.drawImage(canvas, 0, h);
}

Crosshair Grid决赛

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM