简体   繁体   English

canvas 上的绘图点不适用于 safari

[英]drawing point on canvas not working on safari

I would like to use canvas to draw points on mouseover: my implementation works fine on chrome and firefox, but for some reason it doesn't work on safari (however, drawing a line on mouseover works on safari)我想使用 canvas 在鼠标悬停上画点:我的实现在 chrome 和 firefox 上工作正常,但由于某种原因它在 safari 上不起作用(但是,在鼠标悬停上画一条线在 safari 上工作)

any help would be appreciated!任何帮助,将不胜感激!

---> https://draw-9.superhi.com/ ---> https://draw-9.superhi.com/

code:代码:

let canvas = document.querySelector("canvas");

let x = 0;
let y = 0;


let context = canvas.getContext("2d");
context.strokeStyle = "black";
context.lineWidth = 15;
context.lineCap = "round";

canvas.addEventListener("mousemove", function(e) {
  
    x = e.offsetX;
    y = e.offsetY;

  context.moveTo(x, y);
   context.lineTo(x, y);
    context.stroke();
});

In accordance withthe specs , Safari does根据规范,Safari

  1. Prune all zero-length line segments from path .修剪路径中的所有零长度线段
  2. Remove from path any subpaths containing no lines (ie subpaths with just one point).路径中删除任何不包含任何行的子路径(即只有一个点的子路径)。

before tracing it.在追踪它之前。

Your path is made entirely of such zero-length line segments ( moveTo(x,y); lineTo(x,y) ), and hence nothing is left to be drawn.您的路径完全由这样的零长度线段组成( moveTo(x,y); lineTo(x,y) ),因此没有任何东西可以绘制。

Other browsers are buggy here: Firefox bug report , Chrome's .其他浏览器在这里有问题: Firefox 错误报告Chrome 的.

To do what you want, ie draw a circle, use the arc() method.做你想做的,即画一个圆,使用arc()方法。 Also, don't forget to call beginPath() otherwise your path will just grow and you'll end up stroking at the same place over and over leading to bad graphics and performances.另外,不要忘记调用beginPath()否则你的路径只会增长,你最终会一遍又一遍地在同一个地方抚摸,导致糟糕的图形和性能。

 const canvas = document.querySelector("canvas"); let x = 0; let y = 0; const radius = 7.5; const context = canvas.getContext("2d"); context.strokeStyle = "black"; canvas.addEventListener("mousemove", function(e) { x = e.offsetX; y = e.offsetY; context.beginPath(); context.moveTo(x + radius, y); context.arc(x, y, radius, 0, Math.PI*2); context.fill(); });
 <canvas width="900" height="900">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM