<canvas>是H5中最受欢迎的元素,在页面上划出一片区域,利用JS在上面画出图形,起初由Apple公司提出,各大浏览器厂商也对其作了不一样程度上的实现。canvas中规定了了2D context和3D context(WebGL),目前绝大部分浏览器支持2D context。WebGL的发展还比较缓慢。javascript
一、toDataURL()
将画好的图像输出为图片css
//get data URI of the image var imgURI = drawing.toDataURL("image/png"); //display the image var image = document.createElement("img"); image.src = imgURI; document.body.appendChild(image);
二、原点是基于canvas元素左上角
三、2D Context的两个基本绘画操做 fill and strokehtml
一、fillRect()
html5
context.fillRect(10, 10, 50, 50); //draw a blue rectangle that’s semi-transparent context.fillStyle = "rgba(0,0,255,0.5)"; context.fillRect(30, 30, 50, 50);
二、strokeRect()
java
//draw a red outlined rectangle context.strokeStyle = "#ff0000"; context.strokeRect(10, 10, 50, 50); //draw a blue outlined rectangle that’s semi-transparent context.strokeStyle = "rgba(0,0,255,0.5)"; context.strokeRect(30, 30, 50, 50);
三、clearRect()
web
//draw a red rectangle context.fillStyle = "#ff0000"; context.fillRect(10, 10, 50, 50); //draw a blue rectangle that’s semi-transparent context.fillStyle = "rgba(0,0,255,0.5)"; context.fillRect(30, 30, 50, 50); //clear a rectangle that overlaps both of the previous rectangles context.clearRect(40, 40, 10, 10);
一、如何画一个表盘canvas
var drawing = document.getElementById("drawing"); //make sure <canvas> is completely supported if(drawing.getContext) { var context = drawing.getContext("2d"); //start the path context.beginPath(); //draw outer circle context.arc(100, 100, 99, 0, 2 * Math.PI, false); //draw inner circle context.moveTo(194, 100); //将光标移动这个内圆绘制的起点上 context.arc(100, 100, 94, 0, 2 * Math.PI, false); //draw minute hand context.moveTo(100, 100); context.lineTo(100, 15); // 从最后绘制点到(100,15)绘制一条线 //draw hour hand context.moveTo(100, 100); context.lineTo(35, 100); //stroke the path context.stroke(); }
二、判断一个坐标点是否在绘制路线上
context.isPointInPath(100, 100) // true浏览器
一、fillText() 、 strokeText() 后者不多用
二、3个属性font、 textAlign、 textBaseline
三、Demoapp
context.font = "bold 14px Arial"; context.textAlign = "center"; context.textBaseline = "middle"; context.fillText("12", 100, 20); //start-aligned context.textAlign = "start"; context.fillText("12", 100, 40); //end-aligned context.textAlign = "end"; context.fillText("12", 100, 60);
四、measureText()的使用 能够度量文字大小字体
// 若是文字的长度大于140,就将字体缩小 var fontSize = 100; context.font = fontSize + "px Arial"; while(context.measureText("Hello world!").width > 140) { fontSize--; context.font = fontSize + "px Arial"; } context.fillText("Hello world!", 10, 10); context.fillText("Font size is " + fontSize + "px", 10, 50);
1.图像变换方法
rotate(angle)
scale(scaleX, scaleY)
translate(x, y)
transform(m1_1, m1_2, m2_1, m2_2, dx, dy)
setTransform(m1_1, m1_2, m2_1, m2_2, dx, dy)
2.可用translate从新定义原点的位置,
在画圆的时候,可将原点位置移动圆心位置,这样在画圆定位过程当中比较方便
3.context.rotate 注意是将坐标轴进行旋转
var image = document.images[0]; context.drawImage(image, 10, 10);
context.drawImage(image, 10, 10,90,90)
//从图像上(0,10)开始,宽150,高385的区域插入到 canvas中从(0,100)开始,宽40,高60的区域内 context.drawImage(image, 0, 10, 150, 385, 0, 100, 40, 60);
DEMO:
var drawing = document.getElementById("drawing"); var context = drawing.getContext("2d"); context.shadowOffsetX = 5; context.shadowOffsetY = 5; context.shadowBlur = 4; context.shadowColor = "rgba(0, 0, 0, 0.5)"; //draw a red rectangle context.fillStyle = "#ff0000"; context.fillRect(10, 10, 50, 50); //draw a blue rectangle context.fillStyle = "rgba(0,0,255,1)"; context.fillRect(30, 30, 50, 50);
var gradient = context.createLinearGradient(30, 30, 80, 80); gradient.addColorStop(0, "white"); gradient.addColorStop(1, "black"); context.fillStyle = gradient; context.fillRect(30, 30, 50, 50);
var gradient = context.createRadialGradient(55, 55, 10, 55, 55, 30); gradient.addColorStop(0, "white"); gradient.addColorStop(1, "black"); context.fillStyle = gradient; context.fillRect(30, 30, 50, 50);
var context = drawing.getContext("2d"); var image = document.images[0], pattern = context.createPattern(image, "repeat"); //draw a rectangle context.fillStyle = pattern; // 图像repeat模式仍然是从(0,0)开始的 context.fillRect(30, 0, 150, 150); // 这里的意思是绘制矩形,并让图像repeat模式在该矩形区域显示,并非说 图片repeat是从绘制该矩形的起点开始的,渐变也是如此
能够用来作滤镜效果,详细可看 www.html5rocks.com/en/tutorials/canvas/imagefilters/
两个图像之间如何纠缠的,这里举一个例子,其余雷同
var drawing = document.getElementById("drawing"); var context = drawing.getContext("2d"); //draw a red rectangle context.fillStyle = "#ff0000"; context.fillRect(10, 10, 50, 50); //change the global alpha context.globalCompositeOperation = "xor"; //draw a blue rectangle context.fillStyle = "rgba(0,0,255,1)"; context.fillRect(30, 30, 50, 50);