javascript - 负坐标如何在 HTML5 Canvas 中工作?

标签 javascript html html5-canvas

function Background() {
    this.speed = 1; // Redefine speed of the background for panning

   // Implement abstract function
   this.draw = function() {
        // Pan background
        this.y += this.speed;
        this.context.drawImage(imageRepository.background, this.x, this.y);

        // Draw another image at the top edge of the first image
        this.context.drawImage(imageRepository.background, this.x, this.y - this.canvasHeight);

        // If the image scrolled off the screen, reset
        if (this.y >= this.canvasHeight)
            this.y = 0;
    };
}

我试图理解上面的代码,它给出了在无限循环中渲染背景图像的逻辑(给人一种连续平移的错觉)。

我无法理解以下行:

 this.context.drawImage(imageRepository.background, this.x, this.y - this.canvasHeight);

显然 this.y - this.canvasHeight 永远不会 > 0。 Canvas 如何解释负 y 坐标?或者简单地说,下面的代码会做什么?

ctx.drawImage(img, 0, -10);

最佳答案

它根据原点从 -10 开始绘制 y 位置。

即:假设默认原点 0,0(左、上)离 y 轴 10 个像素将不可见,或者您可以认为它是在离屏幕 10 个像素处开始 y。

(将评论转换为答案)

关于javascript - 负坐标如何在 HTML5 Canvas 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17818661/

相关文章:

javascript - Canvas 请求动画在 slider 输入到 JavaScript 变量时超出框架

javascript - JavaScript 中的子像素抗锯齿 Canvas 像素移位算法

javascript - 无法读取 null 和其他 null 变量的属性 'appendChild'

javascript - 如何将我的 muse js 集成到我的 ionic 3 项目中

html - flex-basis 对响应式 flex 表的影响

javascript - 如何在 CSS 的选择输入字段中制作向上/向下箭头?

javascript - 使用 JavaScript 单击时出现错误的 Canvas 动画

javascript - 我的语法错误,请告知 JavaScript 和 JQuery 的适当语法?

javascript - Vue.js - 单向数据绑定(bind)从子级更新父级

html - 将 div 放置在不同宽度的图像内