<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;"><!--style属性可以定义画布的边框--> Your browser does not support the HTML5 canvas tag. </canvas>
##1.2. Draw Onto The Canvas With JavaScript
1 2 3 4 5 6 7 8 9 10 11 12
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;"> Your browser does not support the HTML5 canvas tag. </canvas>
<script><!--插入JavaScript代码-->
var c = document.getElementById("myCanvas");//找到画布元素 var ctx = c.getContext("2d"); //调用getContext()函数,返回内建的html5对象 ctx.fillStyle = "#FF0000"; // 其属性可以是a CSS color, a gradient, or a pattern ctx.fillRect(0,0,150,75);//画出一个矩形
// createLinearGradient() <canvas id="myCanvas" width="200" height="100" style="border:1px solid #d3d3d3;"> Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d");
// Create gradient var grd = ctx.createLinearGradient(0,0,200,0); grd.addColorStop(0,"red"); grd.addColorStop(1,"white");
// Fill with gradient ctx.fillStyle = grd; ctx.fillRect(10,10,150,80);
</script>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//createRadialGradient() <canvas id="myCanvas" width="200" height="100" style="border:1px solid #d3d3d3;"> Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d");
// Create gradient var grd = ctx.createRadialGradient(75,50,5,90,60,100); grd.addColorStop(0,"red"); grd.addColorStop(1,"white");
// Fill with gradient ctx.fillStyle = grd; ctx.fillRect(10,10,150,80);
##1.7. Canvas - Images To draw an image on a canvas, we will use the following method:
drawImage(image,x,y)
在画布上画图,我们使用下面的方法 :drawImage(image,x,y)
1 2 3 4 5 6 7 8 9 10 11 12 13
<p>Image to use:</p> <img id="scream" src="img_the_scream.jpg" alt="The Scream" width="220" height="277"><p>Canvas:</p> <canvas id="myCanvas" width="250" height="300" style="border:1px solid #d3d3d3;"> Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); var img = document.getElementById("scream"); ctx.drawImage(img,10,10);
<svg width="300" height="200"> <polygon points="100,10 40,198 190,78 10,78 160,198" style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;" /> Sorry, your browser does not support inline SVG. </svg>