javascript - 向 Canvas 面孔添加警报

标签 javascript html canvas html5-canvas

我目前正在尝试这样做,以便当您单击其中一张笑脸时,您会收到一个警告框,上面写着“感谢您的反馈”,但我目前不确定是否将其纳入我的代码!谢谢!这是 fiddle http://jsfiddle.net/bsjs9/

    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>SmileMore</title>

    </head>
    <body>

    <h1>
                        Bring your Charts to life with HTML5 Canvas</h1>
                </hgroup>
                <p>
                    Rendering Dynamic charts in JS
                </p>
                <div class="smile">
    <canvas id="myDrawing" width="200" height="200" style="border:1px solid #EEE"></canvas>
        <canvas id="canvas" width="200" height="200" style="border:1px solid #EEE"></canvas>   
         </div>

                <script>
                  var FacePainter = function(canvasName)
{
    var canvas = document.getElementById(canvasName);
    var ctx = canvas.getContext("2d");

    var x = canvas.width / 2;
    var y = canvas.height / 2;
    var radius = 75;
    var startAngle = 0;
    var endAngle = 2 * Math.PI;

    function drawFace() {
        ctx.beginPath();
        ctx.arc(x, y, radius, startAngle, endAngle);
        ctx.stroke();
        ctx.fillStyle = "yellow";
        ctx.fill();
    }

    function drawSmile(startAngle, endAngle)
    {
        var x = canvas.width / 2;
        var y = 150;
        var radius = 40;

        ctx.beginPath();
        ctx.arc(x, y, radius, startAngle * Math.PI, endAngle * Math.PI);
        ctx.lineWidth = 7;

        // line color
        ctx.strokeStyle = 'black';
        ctx.stroke();
    }

    function drawEyes() {
        var centerX = 40;
        var centerY = 0;
        var radius = 10;

        // save state
        ctx.save();

        // translate context so height is 1/3'rd from top of enclosing circle
        ctx.translate(canvas.width / 2, canvas.height / 3);

        // scale context horizontally by 50%
        ctx.scale(.5, 1);

        // draw circle which will be stretched into an oval
        ctx.beginPath();
        ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);

        // restore to original state
        ctx.restore();

        // apply styling
        ctx.fillStyle = 'black';
        ctx.fill();
        ctx.lineWidth = 2;
        ctx.strokeStyle = 'black';
        ctx.stroke();

        //left eye
        var centerX = -40;
        var centerY = 0;
        var radius = 10;

        // save state
        ctx.save();

        // translate context so height is 1/3'rd from top of enclosing circle
        ctx.translate(canvas.width / 2, canvas.height / 3);

        // scale context horizontally by 50%
        ctx.scale(.5, 1);

        // draw circle which will be stretched into an oval
        ctx.beginPath();
        ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);

        // restore to original state
        ctx.restore();

        // apply styling
        ctx.fillStyle = 'black';
        ctx.fill();
        ctx.lineWidth = 2;
        ctx.strokeStyle = 'black';
        ctx.stroke();
    }

    this.drawHappyFace = function() {
        drawFace();
        drawEyes();
        drawSmile(1.1, 1.9);
    }

    this.drawSadFace = function() {
        drawFace();
        drawEyes();
        drawSmile(1.9, 1.1);

        ;
    }
}

new FacePainter('canvas').drawHappyFace();
new FacePainter('myDrawing').drawSadFace();



                </script>    

</body>
</html>
</body>
</html>

作为一项额外作业,我想知道是否有人知道如何修复“快乐”的微笑,这有点遥远!谢谢大家!

最佳答案

由于您在自己的 Canvas 上绘制了笑脸,因此您可以简单地在 Canvas 上放置一个 onclick 处理程序。

<canvas id="myDrawing" width="200" height="200" style="border:1px solid #EEE" onclick="alert('thanks');"></canvas>

关于微笑,我在drawSmile中添加了一个新的ofsy参数,它垂直偏移了圆弧原点。

这是updated fiddle .

如果您只想显示警报,当用户点击脸部内部时,您需要获取点击坐标并针对圆圈进行点击测试。您可以在 this fiddle 中看到这一点.

关于javascript - 向 Canvas 面孔添加警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067153/

相关文章:

javascript - 设置和删除只读 JavaScript - 无法同时执行这两项操作

html - 如何防止标题处的 reveal.js 换行

Javascript上传到自己

javascript - 下载前如何设置 Canvas 背景

javascript - 使用canvas.js在单个页面中导出多个图表

python - 当大内容放置在同一行时防止小部件移动

javascript - 使用原型(prototype)继承的javascript代码中的对象生命周期是什么?

javascript - Rails 4 Cocoon Gem + bootstrap 日期选择器

javascript - [] 前没有分号导致 JavaScript 出错

html - CSS轮廓宽度不起作用