1. <small id='kwjM2'></small><noframes id='kwjM2'>

      • <bdo id='kwjM2'></bdo><ul id='kwjM2'></ul>

      <legend id='kwjM2'><style id='kwjM2'><dir id='kwjM2'><q id='kwjM2'></q></dir></style></legend><tfoot id='kwjM2'></tfoot>
    2. <i id='kwjM2'><tr id='kwjM2'><dt id='kwjM2'><q id='kwjM2'><span id='kwjM2'><b id='kwjM2'><form id='kwjM2'><ins id='kwjM2'></ins><ul id='kwjM2'></ul><sub id='kwjM2'></sub></form><legend id='kwjM2'></legend><bdo id='kwjM2'><pre id='kwjM2'><center id='kwjM2'></center></pre></bdo></b><th id='kwjM2'></th></span></q></dt></tr></i><div id='kwjM2'><tfoot id='kwjM2'></tfoot><dl id='kwjM2'><fieldset id='kwjM2'></fieldset></dl></div>

      鼠标点击在画布上画一个圆圈

      时间:2023-06-21

        • <bdo id='yr0wI'></bdo><ul id='yr0wI'></ul>

            <legend id='yr0wI'><style id='yr0wI'><dir id='yr0wI'><q id='yr0wI'></q></dir></style></legend>

            <small id='yr0wI'></small><noframes id='yr0wI'>

                <tbody id='yr0wI'></tbody>
                <i id='yr0wI'><tr id='yr0wI'><dt id='yr0wI'><q id='yr0wI'><span id='yr0wI'><b id='yr0wI'><form id='yr0wI'><ins id='yr0wI'></ins><ul id='yr0wI'></ul><sub id='yr0wI'></sub></form><legend id='yr0wI'></legend><bdo id='yr0wI'><pre id='yr0wI'><center id='yr0wI'></center></pre></bdo></b><th id='yr0wI'></th></span></q></dt></tr></i><div id='yr0wI'><tfoot id='yr0wI'></tfoot><dl id='yr0wI'><fieldset id='yr0wI'></fieldset></dl></div>

                <tfoot id='yr0wI'></tfoot>
                本文介绍了鼠标点击在画布上画一个圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想在鼠标单击时在画布上绘制一个实心(或非实心)圆圈,但我的代码无法正常工作,我已经尝试了几乎所有方法!

                I want to draw a filled (or not filled) circle in a canvas on mouseclick, but I can't get my code to work properly, I've tried pretty much everything!

                这是我的 HTML:

                <div id="images"></div>
                <canvas style="margin:0;padding:0;position:relative;left:50px;top:50px;" id="imgCanvas" width="250" height="250" onclick="draw(e)"></canvas>
                

                和我当前的脚本:

                var canvas = document.getElementById("imgCanvas");
                var context = canvas.getContext("2d");
                
                function createImageOnCanvas(imageId) {
                    canvas.style.display = "block";
                    document.getElementById("images").style.overflowY = "hidden";
                    var img = new Image(300, 300);
                    img.src = document.getElementById(imageId).src;
                    context.drawImage(img, (0), (0)); //onload....
                }
                
                function draw(e) {
                    var pos = getMousePos(canvas, e);
                    posx = pos.x;
                    posy = pos.y;
                    context.fillStyle = "#000000";
                    context.arc(posx, posy, 50, 0, 2 * Math.PI);
                }
                
                function getMousePos(canvas, evt) {
                    var rect = canvas.getBoundingClientRect();
                    return {
                        x: evt.clientX - rect.left,
                        y: evt.clientY - rect.top
                    };
                }
                

                我认为我的问题在于 function draw(e),尽管我对那部分很有信心.

                I think my problem is with function draw(e), even though I feel pretty confident about that part.

                这里是 jsFiddle

                推荐答案

                我已经分叉并更新了你的小提琴以制作一个工作示例:http://jsfiddle.net/ankr/ds9s7/161/

                I have forked and updated your fiddle to make a working example: http://jsfiddle.net/ankr/ds9s7/161/

                除了错误地引用了事件——正如其他人所说——你在绘画时也没有开始或结束你的路径.添加了 context.beginPath()context.fill() 调用

                Besides referencing the event incorrectly - as stated by others - you also did not begin nor end your path when drawing. Added context.beginPath() and context.fill() calls

                这是相关的JS代码

                var canvas = document.getElementById("imgCanvas");
                var context = canvas.getContext("2d");
                
                function draw(e) {
                    var pos = getMousePos(canvas, e);
                    posx = pos.x;
                    posy = pos.y;
                    context.fillStyle = "#000000";
                    context.beginPath();
                    context.arc(posx, posy, 50, 0, 2*Math.PI);
                    context.fill();
                }
                
                function getMousePos(canvas, evt) {
                    var rect = canvas.getBoundingClientRect();
                    return {
                      x: evt.clientX - rect.left,
                      y: evt.clientY - rect.top
                    };
                }
                

                这篇关于鼠标点击在画布上画一个圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Fabric.js 的文本框不换行 下一篇:二进制数组到画布

                相关文章

                <legend id='pbzv7'><style id='pbzv7'><dir id='pbzv7'><q id='pbzv7'></q></dir></style></legend>

                  <small id='pbzv7'></small><noframes id='pbzv7'>

                1. <tfoot id='pbzv7'></tfoot>
                2. <i id='pbzv7'><tr id='pbzv7'><dt id='pbzv7'><q id='pbzv7'><span id='pbzv7'><b id='pbzv7'><form id='pbzv7'><ins id='pbzv7'></ins><ul id='pbzv7'></ul><sub id='pbzv7'></sub></form><legend id='pbzv7'></legend><bdo id='pbzv7'><pre id='pbzv7'><center id='pbzv7'></center></pre></bdo></b><th id='pbzv7'></th></span></q></dt></tr></i><div id='pbzv7'><tfoot id='pbzv7'></tfoot><dl id='pbzv7'><fieldset id='pbzv7'></fieldset></dl></div>
                  • <bdo id='pbzv7'></bdo><ul id='pbzv7'></ul>