首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用canvas HTML绘制"X“字

使用canvas HTML绘制"X“字
EN

Stack Overflow用户
提问于 2012-10-11 16:41:59
回答 3查看 7K关注 0票数 9

单击时,我使用canvas在鼠标位置绘制"X“字,但当我绘制新的"X”字时,旧的"X“字是”加粗的“。

http://jsfiddle.net/darklight27/h4JvJ/

你对我有什么建议吗?谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-11 16:51:38

在开始画线之前,先调用beginPath()

代码语言:javascript
复制
function drawX(x, y) {
    ctx.beginPath();

    ctx.moveTo(x - 20, y - 20);
    ctx.lineTo(x + 20, y + 20);

    ctx.moveTo(x + 20, y - 20);
    ctx.lineTo(x - 20, y + 20);
    ctx.stroke();
}

更新jsFiddle上的代码:http://jsfiddle.net/h4JvJ/23/

票数 15
EN

Stack Overflow用户

发布于 2019-03-27 15:12:50

Stefan上面的答案用两条线画了一个简单的X。如果您想要更粗的X,请使用以下函数:

代码语言:javascript
复制
const drawX = (ctx, shape, x, y, size, width) => {
    // Start at the top left corner and draw an X
    ctx.beginPath();
    x -= size;
    y -= size;
    ctx.moveTo(x, y);
    x += width / 2;
    y -= width / 2;
    ctx.lineTo(x, y);
    x += size;
    y += size;
    ctx.lineTo(x, y);
    x += size;
    y -= size;
    ctx.lineTo(x, y);
    x += width / 2;
    y += width / 2;
    ctx.lineTo(x, y);
    x -= size;
    y += size;
    ctx.lineTo(x, y);
    x += size;
    y += size;
    ctx.lineTo(x, y);
    x -= width / 2;
    y += width / 2;
    ctx.lineTo(x, y);
    x -= size;
    y -= size;
    ctx.lineTo(x, y);
    x -= size;
    y += size;
    ctx.lineTo(x, y);
    x -= width / 2;
    y -= width / 2;
    ctx.lineTo(x, y);
    x += size;
    y -= size;
    ctx.lineTo(x, y);
    x -= size;
    y -= size;
    ctx.lineTo(x, y);
    ctx.stroke();
    ctx.closePath();
    ctx.fillStrokeShape(shape);
};
票数 1
EN

Stack Overflow用户

发布于 2020-11-29 04:35:17

可自定义的十字

代码语言:javascript
复制
 var cursor = document.createElement("canvas"),
      ctx = cursor.getContext("2d");

    cursor.width = 36;
    cursor.height = 36;

    // draw some shape for sake of demo
    ctx.strokeStyle = color;

    ctx.lineWidth = 2;

    var x = cursor.width / 2;
    var y = cursor.height / 2;

    // ctx.moveTo(x, y);
    ctx.arc(x, y, 20, 0, 2 * Math.PI);
    ctx.stroke();

    ctx.beginPath();
    ctx.arc(x, y, 30, 0, 2 * Math.PI);

    let lineLength = 22

    ctx.moveTo(x - lineLength / 2, y - lineLength / 2);
    ctx.lineTo(x + lineLength / 2, y + lineLength / 2);
    ctx.stroke();

    ctx.moveTo(x + lineLength / 2, y - lineLength / 2);
    ctx.lineTo(x - lineLength / 2, y + lineLength / 2);
    ctx.stroke();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12835531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档