canvas实现线性动画的时候带锯齿的问题

编程入门 行业动态 更新时间:2024-10-16 00:16:33

canvas实现线性动画的时候带<a href=https://www.elefans.com/category/jswz/34/1720473.html style=锯齿的问题"/>

canvas实现线性动画的时候带锯齿的问题

canvas实现线性动画的时候带锯齿的问题

解决方法一

1、就是在canvas标签中设置了width=“200”,height="200"之外, 还在外部的CSS样式表中设置了该canvas的宽度为100%.
2、然后在画图时把canvas的的宽度设为手机端的最大像素值, 因为现在的手机端宽度的最大的只有1080像素宽, 所以就把canvas的宽度和高度设为200的6倍也就是1200像素, 按照这个像素画完之后, width:100%。
3、又会把canvas的宽度和高度缩小至父元素的宽和宽那么大, 因此整个canvas被缩小了, 大尺寸的canvas内容被缩小了之后肯定不会产生锯齿现象,解决的原理其实就是画图时候将canvas的宽和高放大一定的倍数。
4、按照放大后的canvas宽和高画图,然后画完之后再将canvas缩小为目标宽和高,这样解决的方法存在的问题是。
在PC端反而锯齿会更明白,只是移动端的效果很好,所以在pc端不需要放大倍数

	<!DOCTYPE html><html lang="zh-cn"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"><title>html5 canvas 画图移动端出现锯齿毛边的解决方法</title><style type="text/css">#canvas{width: 100%;}</style></head><body style="background: url(blue_bj.jpg);"><div style="width: 200px"><canvas id="canvas" width="200" height="200" ></canvas></div></body></html><script type="text/javascript">// 判断是移动还是pcfunction IsPC() {var userAgentInfo = navigator.userAgent,Agents = ["Android", "iPhone","SymbianOS", "Windows Phone","iPad", "iPod"],flag = true;for (var v = 0; v < Agents.length; v++) {if (userAgentInfo.indexOf(Agents[v]) > 0) {flag = false;break;}}return flag;}//PC端和移动端方法倍数的判断var scale = 1;if( !IsPC() ){scale = 6;}var canvas=document.getElementById("canvas");var cxt=canvas.getContext("2d");//画一个空心圆cxt.beginPath();canvas.width = canvas.width*scale;canvas.height = canvas.height*scale;cxt.arc(canvas.width/2,canvas.height/2,canvas.width/2-scale*16,0,360,false);cxt.lineWidth = scale*16;cxt.strokeStyle = "#faff6d";cxt.stroke();cxt.closePath();</script>

方法二

1、使用window.devicePixelRatio设备上物理像素和设备独立像素(device-independent pixels (dips))的比例来设置canvas实际需要放大的倍数,原理与上一种方法一样,
2、区别在于 devicePixelRatio取出的是实际的比例倍数,在pc端显示为1,避免了上种方法PC端不判断同样放大一样倍数画图出现明显锯齿问题,但是devicePixelRatio各个浏览器的兼容性不是很好,这是唯一缺陷,

	<!DOCTYPE html><html lang="zh-cn"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"><title>html5 canvas 画图移动端出现锯齿毛边的解决方法</title></head><body style="background: url(blue_bj.jpg);"><canvas id="canvas" width="200" height="200" ></canvas></body></html><script type="text/javascript">var canvas=document.getElementById("canvas");var cxt=canvas.getContext("2d");//画一个空心圆cxt.beginPath();var width = canvas.width,height=canvas.height;if (window.devicePixelRatio) {canvas.style.width = width + "px";canvas.style.height = height + "px";canvas.height = height * window.devicePixelRatio;canvas.width = width * window.devicePixelRatio;cxt.arc(canvas.width/2,canvas.height/2,canvas.width/2-16 * window.devicePixelRatio,0,360,false);cxt.lineWidth=16 * window.devicePixelRatio;cxt.strokeStyle="#faff6d";cxt.stroke();//画空心圆cxt.closePath();cxt.scale(window.devicePixelRatio, window.devicePixelRatio);}</script>

更多推荐

canvas实现线性动画的时候带锯齿的问题

本文发布于:2024-03-08 09:24:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1720469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:锯齿   线性   动画   canvas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!