html5之canvas(二)

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...

参考链接为:https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLCanvasElement/toDataURL#Example:_Dynamically_change_images

例子:动态改变图片

可以使用鼠标事件来动态改变图片(官方例子中改变图片灰度)。

<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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值