HTML5 Canvas - 如何放大像素?

发布于 2024-10-07 20:53:15 字数 120 浏览 1 评论 0原文

如何放大画布才能看到像素?目前,当我尝试使用scale()函数时,图像始终是抗锯齿的。

属性 mozImageSmoothingEnabled 似乎有效,但它仅适用于 Firefox。

How would one zoom in a Canvas to see the pixels? Currently when I try to use the scale() function the image is always antialiased.

The property mozImageSmoothingEnabled seems to work, but it's only for Firefox.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笑百媚生 2024-10-14 20:53:15

据我所知,浏览器所做的任何重新缩放都会导致平滑的插值。

所以只要你想用 JS 来做这件事,你就必须让 JS 来做所有的工作。这意味着要么找到一个不错的库,要么自己编写一个函数。它可能看起来像这样。但我希望可以让它更快。由于它是最简单的缩放算法之一,因此可能有很多人想出改进方法以使其更快。

function resize(ctx, sx, sy, sw, sh, tx, ty, tw, th) {
    var source = ctx.getImageData(sx, sy, sw, sh);
    var sdata = source.data;

    var target = ctx.createImageData(tw, th);
    var tdata = target.data;

    var mapx = [];
    var ratiox = sw / tw, px = 0;
    for (var i = 0; i < tw; ++i) {
        mapx[i] = 4 * Math.floor(px);
        px += ratiox;
    }

    var mapy = [];
    var ratioy = sh / th, py = 0;
    for (var i = 0; i < th; ++i) {
        mapy[i] = 4 * sw * Math.floor(py);
        py += ratioy;
    }

    var tp = 0;
    for (py = 0; py < th; ++py) {
        for (px = 0; px < tw; ++px) {
            var sp = mapx[px] + mapy[py];
            tdata[tp++] = sdata[sp++];
            tdata[tp++] = sdata[sp++];
            tdata[tp++] = sdata[sp++];
            tdata[tp++] = sdata[sp++];
        }
    }

    ctx.putImageData(target, tx, ty);
}

该函数将在 (sx,sy) 处获取大小为 (sw,sh) 的矩形,将其大小调整为 (tw,th) 并在 (tx,ty) 处绘制。

As far as I know any rescaling done by the browser will result in a smooth interpolation.

So as long as you want to do this with JS you will have to let JS do all the work. This means either finding a nice library or writing a function yourself. It could look like this. But I hope it's possible to make it faster. As it's one of the simplest scaling algorithms there are probably many people who thought up improvements to do it even faster.

function resize(ctx, sx, sy, sw, sh, tx, ty, tw, th) {
    var source = ctx.getImageData(sx, sy, sw, sh);
    var sdata = source.data;

    var target = ctx.createImageData(tw, th);
    var tdata = target.data;

    var mapx = [];
    var ratiox = sw / tw, px = 0;
    for (var i = 0; i < tw; ++i) {
        mapx[i] = 4 * Math.floor(px);
        px += ratiox;
    }

    var mapy = [];
    var ratioy = sh / th, py = 0;
    for (var i = 0; i < th; ++i) {
        mapy[i] = 4 * sw * Math.floor(py);
        py += ratioy;
    }

    var tp = 0;
    for (py = 0; py < th; ++py) {
        for (px = 0; px < tw; ++px) {
            var sp = mapx[px] + mapy[py];
            tdata[tp++] = sdata[sp++];
            tdata[tp++] = sdata[sp++];
            tdata[tp++] = sdata[sp++];
            tdata[tp++] = sdata[sp++];
        }
    }

    ctx.putImageData(target, tx, ty);
}

This function would take the rectangle of size (sw,sh) at (sx,sy), resize it to (tw,th) and draw it at (tx,ty).

浮生面具三千个 2024-10-14 20:53:15

据我所知,规范中没有定义抗锯齿行为,并且取决于浏览器。

您可以尝试的一件事是,如果您使用 CSS 将画布的宽度/高度设置为 300x300,然后将画布的宽度和高度属性设置为 150 和 150,它将显示为“放大”200%。我不确定这是否会触发抗锯齿,但请尝试一下。

As far as I know the antialiasing behavior is not defined in the spec and will depend on the browser.

One thing you could try is if you set the canvas's width/height with CSS into for example 300x300 and then give the canvas width and height attributes of 150 and 150, it will appear "zoomed in" by 200%. I'm not sure whether this will trigger the antialiasing, but do give it a shot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文