Skip to content

Instantly share code, notes, and snippets.

@phi16
Created November 12, 2016 03:30
Show Gist options
  • Save phi16/cbf8a67a6a8354b9c0d10f277c0fbd4f to your computer and use it in GitHub Desktop.
Save phi16/cbf8a67a6a8354b9c0d10f277c0fbd4f to your computer and use it in GitHub Desktop.
Canvas putImageData
<!DOCTYPE html>
<head>
<title>test</title>
<script type="text/javascript">
var cvs2,ctx2;
var cvs,ctx;
var cnt = 0;
function render(){
ctx.fillStyle = "rgb(255,255,255)";
ctx.fillRect(0,0,400,400);
ctx.drawImage(cvs2,0,0,400,400);
let cx = 200 + Math.cos(cnt*Math.PI/60)*100;
let cy = 200 + Math.sin(cnt*Math.PI/60)*100;
ctx.fillStyle = "rgb(255,128,0)";
ctx.fillRect(cx-10,390,20,20);
cnt++;
requestAnimationFrame(render);
}
window.onload = ()=>{
cvs = document.getElementById("canvas");
ctx = cvs.getContext('2d');
cvs2 = document.createElement('canvas');
cvs2.width = 400;
cvs2.height = 400;
ctx2 = cvs2.getContext('2d');
let img = ctx2.createImageData(400,400);
let data = img.data;
let pos = 0;
for(let j=0;j<400;j++){
for(let i=0;i<400;i++){
data[pos++] = i; // R
data[pos++] = j; // G
data[pos++] = 0x7f; // B
data[pos++] = 0x7f; // A
}
}
ctx2.putImageData(img,0,0);
render();
};
</script>
</head>
<body style="background:#333;">
<canvas id="canvas" width=400 height=400></canvas>
<body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment