長方形 strokeRect fillRect clearRect
パス
beginPath
moveTo
lineTo
closePath
stroke
fill
rect
arcTo
quadraticCurveTo
bezierCurveTo
clip
isPointInPath
ラインスタイル lineWidth lineCap lineJoin miterLimit
色・グラデーション・パターン
strokeStyle
fillStyle
color
globalAlpha
addColorStop
createLinearGradient
createRadialGradient
createPattern
シャドウ
shadowColor
shadowOffsetX
shadowOffsetY
shadowBlur
テキスト・フォント
strokeText
fillText
measureText
font
textAlign
textBaseline
ピクセル操作 createImageData getImageData putImageData
変形 scale rotate translate transform setTransform
Canvas メソッド getContext toDataURL
以降のサンプルは、▶ Run ボタンを押すことで、プログラムを実行します。
var canvas = document.getElementById('jsccanvas');
var context = canvas.getContext('2d');
context.clearRect(0, 0, 640, 400);
context.fillStyle='rgb(32, 32, 64)';
context.fillRect(0, 0, 640, 400);
context.beginPath();
context.fillStyle = 'rgba(255, 0, 0, 0.4)';
context.arc(210, 135, 105, 0, Math.PI*2, false);
context.fill();
context.beginPath();
context.fillStyle = 'rgba(0, 255, 0, 0.4)';
context.arc(135, 285, 105, 0, Math.PI*2, false);
context.fill();
context.beginPath();
context.fillStyle = 'rgba(0, 0, 255, 0.4)';
context.arc(285, 285, 105, 0, Math.PI*2, false);
context.fill();
context.beginPath();
context.strokeStyle='rgb(255, 255, 255)';
context.moveTo(0, 0);
context.lineTo(640, 400);
context.stroke();
context.beginPath();
context.strokeStyle='rgb(255, 255, 0)';
context.moveTo(500, 50);
context.lineTo(501, 51);
context.stroke();
context.beginPath();
context.fillStyle = 'rgb(128, 0, 128)';
context.moveTo(500, 100);
context.lineTo(450, 150);
context.lineTo(550, 150);
context.fill();
context.fillStyle = 'rgb(255, 255, 255)';
context.fillText("GraphicArea (640, 400)", 530, 390);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.strokeRect(400, 200, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.fillStyle = 'rgb(255, 255, 0)';
cx.fillRect(400, 200, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.fillStyle = 'rgb(255, 255, 0)';
cx.fillRect(350, 150, 200, 200);
cx.clearRect(400, 200, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.fillStyle = 'rgb(255, 255, 0)';
cx.beginPath();
cx.moveTo(400, 100);
cx.lineTo(400, 150);
cx.lineTo(600, 150);
cx.lineTo(600, 100);
cx.stroke();
cx.beginPath();
cx.moveTo(400, 200);
cx.lineTo(400, 250);
cx.lineTo(600, 250);
cx.lineTo(600, 200);
cx.closePath();
cx.stroke();
cx.beginPath();
cx.moveTo(400, 300);
cx.lineTo(400, 350);
cx.lineTo(600, 350);
cx.lineTo(600, 300);
cx.fill();
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.fillStyle = 'rgb(255, 255, 0)';
cx.beginPath();
cx.rect(400, 100, 100, 100);
cx.stroke();
cx.beginPath();
cx.rect(400, 250, 100, 100);
cx.fill();
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.beginPath();
cx.moveTo(400, 100);
cx.arcTo(500, 100, 500, 200, 50);
cx.lineTo(500,200);
cx.stroke();
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.beginPath();
cx.moveTo(400, 100);
cx.quadraticCurveTo(500, 100, 500, 200);
cx.stroke();
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.beginPath();
cx.moveTo(400, 100);
cx.bezierCurveTo(480, 100, 500, 120, 500, 200);
cx.stroke();
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.fillStyle = 'rgb(255, 255, 0)';
cx.beginPath();
cx.moveTo(400, 100);
cx.lineTo(300, 300);
cx.lineTo(500, 300);
cx.stroke();
cx.clip();
cx.fillRect(350, 150, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.beginPath();
cx.rect(400, 200, 200, 200);
cx.stroke();
console.log(cx.isPointInPath(500, 300));
console.log(cx.isPointInPath(100, 100));
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.fillStyle = 'rgb(255, 255, 0)';
cx.beginPath();
cx.arc(400, 150, 50, 0, 2*Math.PI, true);
cx.stroke();
cx.beginPath();
cx.arc(550, 150, 50, 0, Math.PI, true);
cx.stroke();
cx.beginPath();
cx.arc(400, 300, 50, 0, 2*Math.PI, true);
cx.fill();
cx.beginPath();
cx.arc(550, 300, 50, 0, Math.PI, false);
cx.fill();
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.lineWidth = 10;
cx.beginPath();
cx.moveTo(400, 100);
cx.lineTo(600, 100);
cx.stroke();
cx.lineWidth = 20;
cx.beginPath();
cx.moveTo(400, 150);
cx.lineTo(600, 150);
cx.stroke();
cx.lineWidth = 50;
cx.beginPath();
cx.moveTo(400, 300);
cx.lineTo(600, 300);
cx.stroke();
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.lineWidth = 30;
cx.lineCap = 'butt';
cx.beginPath();
cx.moveTo(400, 100);
cx.lineTo(600, 100);
cx.stroke();
cx.lineCap = 'round';
cx.beginPath();
cx.moveTo(400, 200);
cx.lineTo(600, 200);
cx.stroke();
cx.lineCap = 'square';
cx.beginPath();
cx.moveTo(400, 300);
cx.lineTo(600, 300);
cx.stroke();
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.lineWidth = 20;
cx.lineJoin = 'bevel';
cx.beginPath();
cx.moveTo(400, 150);
cx.lineTo(500, 50);
cx.lineTo(600, 150);
cx.stroke();
cx.lineJoin = 'round';
cx.beginPath();
cx.moveTo(400, 250);
cx.lineTo(500, 150);
cx.lineTo(600, 250);
cx.stroke();
cx.lineJoin = 'miter';
cx.beginPath();
cx.moveTo(400, 350);
cx.lineTo(500, 250);
cx.lineTo(600, 350);
cx.stroke();
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.lineWidth = 20;
cx.miterLimit = 2.0;
cx.beginPath();
cx.moveTo(400, 150);
cx.lineTo(500, 50);
cx.lineTo(600, 150);
cx.stroke();
cx.beginPath();
cx.moveTo(420, 250);
cx.lineTo(500, 150);
cx.lineTo(580, 250);
cx.stroke();
cx.beginPath();
cx.moveTo(450, 350);
cx.lineTo(500, 250);
cx.lineTo(550, 350);
cx.stroke();
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.fillStyle = '#ff0000';
cx.fillRect(400, 50, 50, 50);
cx.fillStyle = 'rgb(255, 255, 0)';
cx.fillRect(400, 100, 50, 50);
cx.fillStyle = 'rgba(255, 255, 0, 0.5)';
cx.fillRect(400, 150, 50, 50);
cx.fillStyle = 'hsl(120, 50%, 50%)';
cx.fillRect(400, 200, 50, 50);
cx.fillStyle = 'hsla(120, 50%, 50%, 0.5)';
cx.fillRect(400, 250, 50, 50);
cx.fillStyle = 'Brown';
cx.fillRect(400, 300, 50, 50);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.fillStyle = 'rgb(255, 255, 0)';
cx.fillRect(400, 50, 100, 100);
cx.globalAlpha = 0.5;
cx.fillStyle = 'rgb(0, 255, 255)';
cx.fillRect(450, 100, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
var gradient = cx.createLinearGradient(400, 100, 500, 200);
gradient.addColorStop(0, 'rgb(255, 255, 0)');
gradient.addColorStop(1, 'rgb(0, 255, 255)');
cx.fillStyle = gradient;
cx.fillRect(400, 100, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
var gradient = cx.createRadialGradient(500, 200, 0, 500, 250, 100);
gradient.addColorStop(0, 'rgb(255, 255, 0)');
gradient.addColorStop(1, 'rgb(0, 255, 255)');
cx.fillStyle = gradient;
cx.fillRect(400, 100, 200, 200);
テスト画像
var cx = document.getElementById('jsccanvas').getContext('2d');
var img = document.getElementById('testimage');
var pattern = cx.createPattern(img, 'repeat');
cx.fillStyle = pattern;
cx.fillRect(400, 100, 200, 200);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.fillStyle = 'rgb(255, 255, 0)';
cx.shadowColor = 'gray';
cx.shadowOffsetX = 10;
cx.shadowOffsetY = 10;
cx.shadowBlur = 5;
cx.fillRect(400, 100, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(0, 255, 255)';
cx.fillStyle = 'rgb(255, 255, 0)';
cx.font = '40px serif';
cx.strokeText('abc漢字', 400, 100);
cx.strokeText('abc漢字', 400, 150, 50);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(0, 255, 255)';
cx.fillStyle = 'rgb(255, 255, 0)';
cx.font = '40px serif';
cx.fillText('abc漢字', 400, 100);
cx.fillText('abc漢字', 400, 150, 50);
var cx = document.getElementById('jsccanvas').getContext('2d');
console.log(cx.measureText('abcde漢字').width);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.fillStyle = 'rgb(255, 255, 0)';
cx.font = '20px "MS ゴシック"';
cx.fillText('abcde漢字', 400, 100);
cx.font = 'bold 20px "MS 明朝"';
cx.fillText('abcde漢字', 400, 150);
cx.font = 'italic small-caps bold 20px arial';
cx.fillText('abcde漢字', 400, 200);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 0, 0)';
cx.fillStyle = 'rgb(255, 255, 0)';
cx.beginPath();
cx.moveTo(500, 50);
cx.lineTo(500, 400);
cx.stroke();
cx.font = '20px serif';
cx.fillText('abcdefg', 500, 100);
cx.fillText('abcd', 500, 120);
cx.textAlign = 'start';
cx.fillText('abcdefg', 500, 150);
cx.fillText('abcd', 500, 170);
cx.textAlign = 'end';
cx.fillText('abcdefg', 500, 200);
cx.fillText('abcd', 500, 220);
cx.textAlign = 'left';
cx.fillText('abcdefg', 500, 250);
cx.fillText('abcd', 500, 270);
cx.textAlign = 'right';
cx.fillText('abcdefg', 500, 300);
cx.fillText('abcd', 500, 320);
cx.textAlign = 'center';
cx.fillText('abcdefg', 500, 350);
cx.fillText('abcd', 500, 370);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 0, 0)';
cx.fillStyle = 'rgb(255, 255, 0)';
cx.beginPath();
cx.moveTo( 10, 350);
cx.lineTo(640, 350);
cx.stroke();
cx.font = '20px serif';
cx.fillText('Noxyz', 20, 350);
cx.textBaseline = 'top';
cx.fillText('TopXyz', 70, 350);
cx.textBaseline = 'bottom';
cx.fillText('BottomXyz', 100, 350);
cx.textBaseline = 'middle';
cx.fillText('MiddleXyz', 195, 350);
cx.textBaseline = 'alphabetic';
cx.fillText('AlphabeticXyz', 290, 350);
cx.textBaseline = 'hanging';
cx.fillText('HangingXyz', 410, 350);
cx.textBaseline = 'ideographic';
cx.fillText('IdeographicXyz', 510, 350);
var cx = document.getElementById('jsccanvas').getContext('2d');
var img = document.getElementById('testimage');
cx.drawImage(img, 500, 100);
cx.drawImage(img, 500, 200, 80, 40);
cx.drawImage(img, 10, 20, 80, 20, 500, 300, 100, 50);
var cx = document.getElementById('jsccanvas').getContext('2d');
var imagedata = cx.createImageData(256, 256);
for(var i=0; i < imagedata.data.length; i += 4) {
imagedata.data[i+0] = (i / 4) % 256;
imagedata.data[i+1] = (i / 4 / 256) % 256;
imagedata.data[i+2] = 255 - imagedata.data[i+1];
imagedata.data[i+3] = 128;
}
cx.putImageData(imagedata, 50, 100);
var imageget = cx.getImageData(50, 100, 256, 256);
for(var i=0; i < imageget.data.length; i += 4) {
var v = (imageget.data[i+0]+imageget.data[i+1]+imageget.data[i+2])/3;
imageget.data[i+0] = v;
imageget.data[i+1] = v;
imageget.data[i+2] = v;
imageget.data[i+3] = imageget.data[i+3];
}
cx.putImageData(imageget, 350, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.strokeRect(10, 10, 100, 100);
cx.scale(2, 1);
cx.strokeRect(10, 10, 100, 100);
cx.scale(1, 2);
cx.strokeRect(10, 10, 100, 100);
cx.scale(2, 2);
cx.strokeRect(10, 10, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.strokeRect(400, 50, 100, 100);
cx.rotate(Math.PI / 12);
cx.strokeRect(400, 50, 100, 100);
cx.rotate(Math.PI / 12);
cx.strokeRect(400, 50, 100, 100);
cx.rotate(Math.PI / 12);
cx.strokeRect(400, 50, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.strokeRect(10, 10, 100, 100);
cx.translate(100, 100);
cx.strokeRect(10, 10, 100, 100);
cx.translate(100, 100);
cx.strokeRect(10, 10, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.strokeRect(10, 10, 100, 100);
cx.transform(1.5, 0.5, -0.5, 1.5, 50, 50);
cx.strokeRect(10, 10, 100, 100);
cx.transform(1.5, 0.5, -0.5, 1.5, 50, 50);
cx.strokeRect(10, 10, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.strokeRect(10, 10, 100, 100);
cx.setTransform(1.5, 0.5, -0.5, 1.5, 50, 50);
cx.strokeRect(10, 10, 100, 100);
cx.setTransform(1.5, 0.5, -0.5, 1.5, 50, 50);
cx.strokeRect(10, 10, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.strokeStyle = 'rgb(255, 255, 0)';
cx.strokeRect(10, 10, 100, 100);
cx.save();
cx.strokeStyle = 'rgb(0, 255, 255)';
cx.setTransform(1.5, 0.5, -0.5, 1.5, 50, 50);
cx.strokeRect(10, 10, 100, 100);
cx.restore();
cx.strokeRect(20, 20, 100, 100);
var cx = document.getElementById('jsccanvas').getContext('2d');
cx.fillStyle = 'rgb(255, 255, 0)';
cx.fillRect(300, 100, 200, 200);
cx.fillStyle = 'rgb(0, 255, 255)';
//cx.globalCompositeOperation = 'source-atop';
//cx.globalCompositeOperation = 'source-in';
//cx.globalCompositeOperation = 'source-out';
cx.globalCompositeOperation = 'source-over';
//cx.globalCompositeOperation = 'destination-atop';
//cx.globalCompositeOperation = 'destination-in';
//cx.globalCompositeOperation = 'destination-out';
//cx.globalCompositeOperation = 'destination-over';
//cx.globalCompositeOperation = 'lighter';
//cx.globalCompositeOperation = 'copy';
//cx.globalCompositeOperation = 'xor';
cx.fillRect(400, 200, 200, 200);
var canvas = document.getElementById('jsccanvas');
var str = canvas.toDataURL();
console.log(str);