第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

創(chuàng)建圖像來繪制東西,然后在畫布上繪制圖像

創(chuàng)建圖像來繪制東西,然后在畫布上繪制圖像

慕村9548890 2023-04-01 16:31:36
大家好!是否可以使用 JavaScript 創(chuàng)建圖像然后在其上渲染形狀,然后將其繪制到游戲畫布上?無需使用 dataurl、url 或 src,就可以了!var ctx = document.getElementById("canvas").getContext("2d");var img = new Image();// TODO: Draw stuff to the image imgfunction game() {    window.requestAnimationFrame(game);    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);    ctx.drawImage(img, 0, 0, 256, 256);}window.requestAnimationFrame(game);
查看完整描述

1 回答

?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊

CanvasRenderingContext2D.drawImage()功能可以將多種類型的圖像作為源,包括另一個(gè) Canvas。下面的示例顯示圖像已加載到第一個(gè)畫布中。然后你可以通過按住鼠標(biāo)并移動(dòng)它來繪制它。當(dāng)您釋放時(shí),第二個(gè)畫布將繪制第一個(gè)畫布當(dāng)時(shí)的圖像。

所有的魔法都在最后一個(gè)函數(shù)中。

contextTwo.drawImage(canvasOne,?0,?0,?256,?256);

const canvasOne = document.getElementById('canvas1');

const canvasTwo = document.getElementById('canvas2');

const contextOne = canvasOne.getContext('2d');

const contextTwo = canvasTwo.getContext('2d');


canvasOne.width = 256;

canvasOne.height = 256;


canvasTwo.width = 256;

canvasTwo.height = 256;


const canvasBounds = canvasOne.getBoundingClientRect();


let mouseData = {

? isClicked: false,

? position: [0, 0],

}


const onMouseDown = event => {

? mouseData.isClicked = true;

? render();

};


const onMouseMove = ({ clientX, clientY }) => {

? const x = clientX - canvasBounds.left;

? const y = clientY - canvasBounds.top;

? mouseData.position = [x, y];

? render();

};


const onMouseUp = event => {

? mouseData.isClicked = false;

? render();

? moveImage();

};


function setup() {

? const img = new Image();

? img.src = '//picsum.photos/256/256'

? img.onload = function() {

? ? contextOne.drawImage(img, 0, 0, 256, 256);

? }

??

? canvasOne.addEventListener('mousedown', onMouseDown);

? canvasOne.addEventListener('mousemove', onMouseMove);

? canvasOne.addEventListener('mouseup', onMouseUp);

}


function render() {

? requestAnimationFrame(() => {

? ? const { isClicked, position } = mouseData;

? ? const [ x, y ] = position;

? ? if (isClicked) {

? ? ? contextOne.beginPath();

? ? ? contextOne.arc(x, y, 5, 0, Math.PI * 2)

? ? ? contextOne.fillStyle = 'red'

? ? ? contextOne.fill();

? ? ? contextOne.closePath();

? ? }

? });

}


function moveImage() {

? contextTwo.drawImage(canvasOne, 0, 0, 256, 256);

}


setup();

body {

? display: flex;

}


canvas {

? width: 256px;

? height: 256px;

? border: 1px solid #d0d0d0;

}

<canvas id="canvas1"></canvas>

<canvas id="canvas2"></canvas>


查看完整回答
反對(duì) 回復(fù) 2023-04-01
  • 1 回答
  • 0 關(guān)注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)