canvas 之画弧
canvas是基于状态:状态就是变量不同时期的值。
ctx=cvs.getContext(‘2d’);
其中ctx中有一个属性:ctx.canvas.height…
canvas就是获取到的画布
HTMLCanvasElement.toDataURL()
canvas.toDataURL(type, encoderOptions);
type 可选 图片格式,默认为 image/png
encoderOptions 可选 在指定图片格式为 image/jpeg 或 image/webp的情况下,可以从 0 到 1 的区间内选择图片的质量。如果超出取值范围,将会使用默认值 0.92。其他参数会被忽略。
<canvas id="canvas" width="5" height="5"></canvas>
var canvas = document.getElementById("canvas");
var dataURL = canvas.toDataURL("image/jpeg", 1.0);
console.log(dataURL);
// "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNby...
例子:动态改变图片
可以使用鼠标事件来动态改变图片(官方例子中改变图片灰度)。
<img class="grayscale" src="myPicture.png" alt="Description of my picture" />
window.addEventListener("load", removeColors);
function showColorImg() {
this.style.display = "none";
this.nextSibling.style.display = "inline";
}
function showGrayImg() {
this.previousSibling.style.display = "inline";
this.style.display = "none";
}
function removeColors() {
var aImages = document.getElementsByClassName("grayscale"),
nImgsLen = aImages.length,
oCanvas = document.createElement("canvas"),
oCtx = oCanvas.getContext("2d");
for (var nWidth, nHeight, oImgData, oGrayImg, nPixel, aPix, nPixLen, nImgId = 0; nImgId < nImgsLen; nImgId++) {
oColorImg = aImages[nImgId];
nWidth = oColorImg.offsetWidth;
nHeight = oColorImg.offsetHeight;
oCanvas.width = nWidth;
oCanvas.height = nHeight;
oCtx.drawImage(oColorImg, 0, 0);
oImgData = oCtx.getImageData(0, 0, nWidth, nHeight);
aPix = oImgData.data;
nPixLen = aPix.length;
for (nPixel = 0; nPixel < nPixLen; nPixel += 4) {
aPix[nPixel + 2] = aPix[nPixel + 1] = aPix[nPixel] = (aPix[nPixel] + aPix[nPixel + 1] + aPix[nPixel + 2]) / 3;
}
oCtx.putImageData(oImgData, 0, 0);
oGrayImg = new Image();
oGrayImg.src = oCanvas.toDataURL();
oGrayImg.onmouseover = showColorImg;
oColorImg.onmouseout = showGrayImg;
oCtx.clearRect(0, 0, nWidth, nHeight);
oColorImg.style.display = "none";
oColorImg.parentNode.insertBefore(oGrayImg, oColorImg);
}
}
其中getImageData()方法在运行的过程中会报错,原因是该方法涉及到跨域问题,
最简单的解决办法是用火狐浏览器打开该页面。
看到的效果是:
参考链接为:https://www.cnblogs.com/lodadssd/p/6238250.html
ctx.arc() 画弧,画的是路径
ctx.arc( 圆心x轴坐标,圆心y轴坐标,半径, 起点弧度,结束点弧度,是否逆时针画(可选) )
arc方法内部会先从路径结束点到弧的起点画一条路径线。
起点弧度、结束点弧度以及弧度的方向共同决定了弧的大小。
画扇形思路
- 1、先设置路径起点为圆心
- 2、画弧
- 3、闭合路径
HTML 5 canvas font 属性
设置文字的属性
ctx.font="40px Arial";
ctx.fillText("Hello World",10,50);
注意:这里设置字体大小时必须带单位,单位支持css的所有表示方式。
注意:单独设置字体大小不会生效,必须要加一个额外属性样式。
HTML5 canvas strokeText() 方法
使用 strokeText() 绘制描边文字,在画布上写文本 “Hello world!”
ctx.font="20px Georgia";
ctx.strokeText("Hello World!",10,50);
ctx.strokeText( 文字, 参考x轴坐标,参考y轴坐标,限制文字的最大长度(可选) )
HTML5 canvas fillText() 方法
使用 fillText() 绘制填充文字,在画布上写文本 “Hello world!”
ctx.font="20px Georgia";
ctx.fillText("Hello World!",10,50);
ctx.fillText( 文字, 参考x轴坐标,参考y轴坐标,限制文字的最大长度(可选) )
HTML 5 canvas textAlign 属性
设置文字的水平对其方式, 默认值为start。
在位置 150 创建一条蓝线。位置 150 是下面例子中定义的所有文本的锚点。
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
// 在位置 150 创建蓝线
ctx.strokeStyle="blue";
ctx.moveTo(150,20);
ctx.lineTo(150,170);
ctx.stroke();
ctx.font="15px Arial";
// 显示不同的 textAlign 值
ctx.textAlign="start";
ctx.fillText("textAlign=start",150,60);
ctx.textAlign="end";
ctx.fillText("textAlign=end",150,80);
ctx.textAlign="left";
ctx.fillText("textAlign=left",150,100);
ctx.textAlign="center";
ctx.fillText("textAlign=center",150,120);
ctx.textAlign="right";
ctx.fillText("textAlign=right",150,140);
ctx.textAlign = ‘left / start / right / end / center’
运行代码效果:
其他方法和属性参考:http://www.w3school.com.cn/tags/html_ref_canvas.asp