<canvas id="flag" width="320" height="220"> You don't support Canvas. If you did, you would see a flag </canvas>
#flag {
width: 320px;
height: 220px;
margin: auto;
}
var el= document.getElementById("flag");
if (el && el.getContext) {
var context = el.getContext('2d');
if(context){
context.fillStyle = "#ffffff";
context.strokeStyle = "#CCCCCC";
context.lineWidth = 5;
context.shadowOffsetX = 5;
context.shadowOffsetY = 5;
context.shadowBlur = 4;
context.shadowColor = 'rgba(0, 0, 0, 0.4)';
context.strokeRect(10, 10, 300, 200);
context.fillRect(10, 10, 300, 200);
context.shadowColor='rgba(0,0,0,0)';
context.fillStyle = "#d60818";
context.arc(160, 107, 60, 0, Math.PI*2, false);
context.fill();
}
}
<canvas id="flag" width="320" height="220"> You don't support Canvas. If you did, you would see a flag </canvas>
var el= document.getElementById("flag");
if (el && el.getContext) {
var context = el.getContext('2d');
if(context){
/* draw here */
}
}
Rectangle is the only shape!
context.fillStyle = "#ffffff"; context.fillRect(10, 10, 300, 200);
you can border that rectangle
context.fillStyle = "#ffffff"; context.strokeStyle = "#CCCCCC"; context.lineWidth = 5; context.strokeRect(10, 10, 300, 200); context.fillRect(10, 10, 300, 200);
Note: only 50% of border will show
Drawing straight lines
Appearance of line / shapes
context.beginPath(); context.moveTo(50,150); context.lineTo(100,100); context.lineTo(150,150); context.lineTo(200,100); context.lineTo(250,150); context.stroke();
arc(x, y, radius, startAngle, endAngle, clockwise);
context.fillStyle = "#d60818"; context.arc(160, 107, 60, 0, Math.PI*2, false); context.fill();
1 if(context){
2 context.fillStyle = "#ffffff";
3 context.strokeStyle = "#CCCCCC";
4 context.lineWidth = 5;
5 context.shadowOffsetX = 5;
6 context.shadowOffsetY = 5;
7 context.shadowBlur = 4;
8 context.shadowColor = 'rgba(0, 0, 0, 0.4)';
9 context.strokeRect(10, 10, 300, 200);
10 context.fillRect(10, 10, 300, 200);
11 context.shadowColor='rgba(0,0,0,0)';
12 context.fillStyle = "#d60818";
13 context.arc(160, 107, 60, 0, Math.PI*2, false);
14 context.fill();
15 }
context.shadowColor = 'rgba(255, 0, 0, 1)'
context.fillStyle = "#000000";
context.textAlign="center";
context.font="italic 20px verdana";
context.fillText("Japanese Flag", 150, 150);
var image=new Image();
image.src="myImage.png";
image.onload=function() {
context.drawImage(image, 140, 0, 206, 183, 0, 0, 206, 183);
}
It is possible to get pixel data from canvas
//draw image
context.drawImage(image,0,0);
//get data from the canvas
var pixels=context.getImageData(0, 0, el.width, el.height);
// loop thru each pixel's 4 colors (length/4)
for (var i = 0, n = pixels.data.length; i < n; i += 4){
//red
pixels.data[i+0] = 0;
//green
pixels.data[i+1] = 255 - pixels.data[i+1];
//blue
pixels.data[i+2] = 0;
// alpha transparency not altered.
}
//draw the image with new pixels
context.putImageData(pixels,0,0);