上图对比,后图明显模糊了。
VS
在PC中可以像这样将width与height设置为css的2倍可以解决retina屏适配,而小程序中不可以,直接将<canvas>中的width与height属性过滤了。
<!DOCTYPE HTML>
<html>
<body>
<canvas id="myCanvas" width="2000" height="2000" style="width:1000px;height:1000px;">your browser does not support the canvas tag123213 </canvas>
<script type="text/javascript">
var canvas=document.getElementById('myCanvas');
var ctx=canvas.getContext('2d');
ctx.fillStyle='#ff6600';
ctx.fillRect(0,0,500,500);
</script>
</body>
</html>
如果对比ppi换算结果不是刚刚2倍的,,可以使用如下的代码来获取ratio值
backingStore = context.backingStorePixelRatio ||
context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 1;
以上代码摘自:https://github.com/jondavidjohn/hidpi-canvas-polyfill
尝试在小程序中去比划比划
<canvas canvas-id="line-canvas" id="line-canvas" tabindex="2" width="750" height="400" style="width: 375px; height: 200px;"></canvas>
结果在Wxml调试窗口成这样了
w3c说得很清楚,有width与height属性的,为什么不走w3c寻常路呢。http://www.w3school.com.cn/html5/html5_canvas.asp
反观微信团队自己出的小程序数据助手却支持retina屏,为这个问题花了1天时间了,不知道要怎么破了。