带有偏移量的 Canvas createPattern() 和 fill()

我使用画布绘制简单的矩形并用地板图案(PNG)填充它们。但是,如果我利用“相机”脚本来处理 HTML5 画布的变换偏移,则矩形形状会适当移动,但填充图案似乎总是从屏幕上的固定点绘制(我假设在左上角)。有没有一种方法可以“确定”填充图案,使其始终与矩形对齐,无论画布变换如何,或者有一种方法可以在 fill() 上添加可以在代码中其他地方计算的偏移量?当然,我可以只使用drawImage(),但是绘制矩形对于我的目的来说更通用。


        sampleDrawFunction(fillTexture, x1, y1, x2, y2) {

        // This is oversimplified, but best I can do with a ~10k lines of code

        x1 -= Camera.posX;

        x2 -= Camera.posX;

        y1 = -1 * y1 - Camera.posY;

        y2 = -1 * y2 - Camera.posY;


        // Coordinates need to be adjusted for where the camera is positioned


        var img = new Image();

        img.src = fillTexture;

        var pattern = this.ctx.createPattern(img, "repeat");

        this.ctx.fillStyle = pattern;


        // Translate canvas's coordinate pattern to match what the camera sees

        this.ctx.save();

        this.ctx.translate(Camera.posX - Camera.topLeftX, Camera.posY - Camera.topLeftY);

        this.ctx.fillStyle = pattern;

        this.ctx.fillRect(x1, y1, x2 - x1, y2 - y1);

        this.ctx.restore();

}

谢谢。


潇潇雨雨
浏览 59回答 1
1回答

小怪兽爱吃肉

画布填充(CanvasPatterns 和 CanvasGradients)始终相对于上下文的变换矩阵,因此它们确实默认位于画布的左上角,并且并不真正关心使用它们的路径在哪里。const canvas = document.getElementById('canvas');const ctx = canvas.getContext('2d');const img = new Image();img.onload = begin; //!\ ALWAYS WAIT FOR YOUR IMAGE TO LOAD BEFORE DOING ANYTHING WITH IT!img.crossOrigin = "anonymous";img.src = "https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png";function begin() {&nbsp; const rect_size = 20;&nbsp; ctx.fillStyle = ctx.createPattern( img, 'no-repeat' );&nbsp; // drawing a checkerboard of several rects shows that the pattern doesn't move&nbsp; for ( let y = 0; y < canvas.height; y += rect_size ) {&nbsp; &nbsp; for ( let x = (y / rect_size % 2) ? rect_size : 0 ; x < canvas.width; x += rect_size * 2 ) {&nbsp; &nbsp; &nbsp; ctx.fillRect( x, y, rect_size, rect_size );&nbsp; &nbsp; }&nbsp; }}<canvas id="canvas" width="500" height="500"></canvas>现在,因为它们与上下文变换矩阵相关,这意味着我们还可以通过更改该变换矩阵来移动它们:const canvas = document.getElementById('canvas');const ctx = canvas.getContext('2d');const img = new Image();img.onload = begin; //!\ ALWAYS WAIT FOR YOUR IMAGE TO LOAD BEFORE DOING ANYTHING WITH IT!img.crossOrigin = "anonymous";img.src = "https://upload.wikimedia.org/wikipedia/commons/f/f7/Cool_bunny_sprite.png";function begin() {&nbsp; const rect_size = 244;&nbsp; const max_speed = 5;&nbsp; let x_speed = Math.random() * max_speed;&nbsp; let y_speed = Math.random() * max_speed;&nbsp; ctx.fillStyle = ctx.createPattern( img, 'repeat' );&nbsp;&nbsp;&nbsp; let x = 0;&nbsp; let y = 0;&nbsp; requestAnimationFrame( anim );&nbsp;&nbsp;&nbsp; function anim( now ) {&nbsp; &nbsp; clear();&nbsp; &nbsp; x = (x + x_speed);&nbsp; &nbsp; y = (y + y_speed);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if( x > canvas.width || x < -rect_size ) {&nbsp; &nbsp; &nbsp; x_speed = Math.random() * max_speed * -Math.sign( x_speed );&nbsp; &nbsp; &nbsp; x = Math.min( Math.max( x, -rect_size ), canvas.width );&nbsp; &nbsp; }&nbsp; &nbsp; if( y > canvas.height || y < -rect_size ) {&nbsp; &nbsp; &nbsp; y_speed = Math.random() * max_speed * -Math.sign( y_speed )&nbsp; &nbsp; &nbsp; y = Math.min( Math.max( y, -rect_size ), canvas.height );&nbsp; &nbsp; }&nbsp; &nbsp; // we change the transformation matrix of our context&nbsp; &nbsp; ctx.setTransform( 1, 0, 0, 1, x, y );&nbsp; &nbsp; // we thus always draw at coords 0,0&nbsp; &nbsp; ctx.fillRect( 0, 0, rect_size, rect_size );&nbsp; &nbsp; ctx.strokeRect( 0, 0, rect_size, rect_size );&nbsp; &nbsp; requestAnimationFrame( anim );&nbsp; }&nbsp; function clear() {&nbsp; &nbsp; // since we changed the tranform matrix we need to reset it to identity&nbsp; &nbsp; ctx.setTransform( 1, 0, 0, 1, 0, 0 );&nbsp; &nbsp; ctx.clearRect( 0, 0, canvas.width, canvas.height );&nbsp; }}<canvas id="canvas" width="300" height="300"></canvas>我们甚至可以通过在声明子路径后更改变换矩阵来将路径声明与填充分离,当然也可以将简写替换 fillRect()为beginPath(); rect(); fill()const canvas = document.getElementById('canvas');const ctx = canvas.getContext('2d');const img = new Image();img.onload = begin; //!\ ALWAYS WAIT FOR YOUR IMAGE TO LOAD BEFORE DOING ANYTHING WITH IT!img.crossOrigin = "anonymous";img.src = "https://upload.wikimedia.org/wikipedia/commons/f/f7/Cool_bunny_sprite.png";function begin() {&nbsp; const rect_size = 244;&nbsp; const max_speed = 5;&nbsp; let x_speed = Math.random() * max_speed;&nbsp; let y_speed = Math.random() * max_speed;&nbsp; ctx.fillStyle = ctx.createPattern( img, 'repeat' );&nbsp;&nbsp;&nbsp; let x = 0;&nbsp; let y = 0;&nbsp; requestAnimationFrame( anim );&nbsp;&nbsp;&nbsp; function anim( now ) {&nbsp; &nbsp; clear();&nbsp; &nbsp; x = (x + x_speed);&nbsp; &nbsp; y = (y + y_speed);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if( x > canvas.width || x < -rect_size ) {&nbsp; &nbsp; &nbsp; x_speed = Math.random() * max_speed * -Math.sign( x_speed );&nbsp; &nbsp; &nbsp; x = Math.min( Math.max( x, -rect_size ), canvas.width );&nbsp; &nbsp; }&nbsp; &nbsp; if( y > canvas.height || y < -rect_size ) {&nbsp; &nbsp; &nbsp; y_speed = Math.random() * max_speed * -Math.sign( y_speed )&nbsp; &nbsp; &nbsp; y = Math.min( Math.max( y, -rect_size ), canvas.height );&nbsp; &nbsp; }&nbsp; &nbsp; // we declare the sub-path first, with identity matrix applied&nbsp; &nbsp; ctx.beginPath();&nbsp; &nbsp; ctx.rect( 50, 50, rect_size, rect_size );&nbsp; &nbsp; // we change the transformation matrix of our context&nbsp; &nbsp; ctx.setTransform( 1, 0, 0, 1, x, y );&nbsp; &nbsp; // and now we fill&nbsp; &nbsp; ctx.fill();&nbsp; &nbsp; ctx.stroke();&nbsp; &nbsp; requestAnimationFrame( anim );&nbsp; }&nbsp; function clear() {&nbsp; &nbsp; // since we changed the tranform matrix we need to reset it to identity&nbsp; &nbsp; ctx.setTransform( 1, 0, 0, 1, 0, 0 );&nbsp; &nbsp; ctx.clearRect( 0, 0, canvas.width, canvas.height );&nbsp; }}<canvas id="canvas" width="300" height="300"></canvas>但就你而言,听起来改变整个绘图是最简单、最惯用的方法。不太确定为什么要修改相对于相机的 x 和 y 坐标。一般来说,如果我们使用相机对象,那么场景中的对象不必关心它,并且与世界保持相对关系。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5