使用canvas
画一个p
的对角线,先算p
的width
和height
,再赋给canvas
的width
和height
,然后建立一个2d
画布开始画,为什么画出的斜线很模糊,改成直线就还好?感觉没有被因为p
和画布的大小不同而放大或缩小
var canvasDom = document.querySelector(".canvas_line");
var w = parseInt($(canvasDom).css("width"));
var h = parseInt($(canvasDom).css("height"));
canvasDom.width = w;
canvasDom.height = h;
var context = canvasDom.getContext('2d');;
context.beginPath();
context.moveTo(0,0);
context.lineTo(w, h);
context.lineWidth = "1";
context.strokeStyle = "red";
context.stroke();
If the device you are using is too high-definition, you can try this:
Use css style (style attribute) on canvas to determine the height and width within the page
Set the width and height attributes of the canvas to 2 times the height and width of the css style
Start enjoying your drawing
Try moveTo(0.5,0.5);
I found an article that I hope will be helpful to you: How to cancel anti-aliased drawing in HTML5 Canvas
Hello, you need to set the width and height attributes in the canvas element. If you define the width and height of canvas in css, the lines will be blurred, so your change JS
Similarly, the height part also needs to be changed in this way