0

我已经尝试了这么多的代码。我有两个圆圈旁边有一个链接,我需要在相应链接的点击上为圆圈着色。提出一些解决方案

<body>
    <div class="row-fluid">
        <div class="span1 offset3">
            <canvas id="myCanvas" width="100" height="100"></canvas>
        </div>
        <div class="span1">
            <br/><a href="#" onclick="function();">Hello</a>
        </div>
        <div class="span1">
            <canvas id="myCanvas1" width="100" height="100"></canvas>
        </div>
        <div class="span1">
            <br/><a href="#" onclick="function();">Hi</a>
        </div>
    </div>
    <script>
        var c = document.getElementById("myCanvas");
        var ctx = c.getContext("2d");
        ctx.beginPath();
        ctx.arc(55, 30, 20, 0, 2 * Math.PI);
        ctx.stroke();

        var c = document.getElementById("myCanvas1");
        var ctx = c.getContext("2d");
        ctx.beginPath();
        ctx.arc(55, 30, 20, 0, 2 * Math.PI);
        ctx.stroke();

        function () {
            //onclick function which will change the color of the correspondind circle  
        }
    </script>
</body>
4

2 回答 2

1

单击html链接时如何填充画布圆圈

在此处输入图像描述

这是一个灵活的函数,它可以绘制一个圆并可选地填充该圆:

    function drawCircle(context,fill){
        context.beginPath();
        context.arc(55, 30, 20, 0, 2 * Math.PI);
        context.closePath();
        context.stroke();
        if(fill){
            context.fill()
        }
    }

jquery 可以监听你的锚点的点击。

然后jquery用fill=true调用drawCircle函数(圆圈被填满)

    // fill the circle on click
    $("#c1").click(function(){ drawCircle(ctx,true); });
    $("#c2").click(function(){ drawCircle(ctx1,true); });

这是代码和小提琴:http: //jsfiddle.net/m1erickson/8LGPB/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; padding:20px; }
    canvas{border:1px solid red;}
</style>

<script>
$(function(){

    var canvas=document.getElementById("myCanvas");
    var ctx=canvas.getContext("2d");
    var canvas1=document.getElementById("myCanvas1");
    var ctx1=canvas1.getContext("2d");

    // draw stroked but not filled circles
    drawCircle(ctx,false);
    drawCircle(ctx1,false);

    function drawCircle(context,fill){
        context.beginPath();
        context.arc(55, 30, 20, 0, 2 * Math.PI);
        context.closePath();
        context.stroke();
        if(fill){
            context.fill()
        }
    }

    // fill the circle on click
    $("#c1").click(function(){ drawCircle(ctx,true); });
    $("#c2").click(function(){ drawCircle(ctx1,true); });


}); // end $(function(){});
</script>

</head>

<body>
<div class="row-fluid">
    <div class="span1 offset3">
        <canvas id="myCanvas" width="100" height="100"></canvas>
    </div>
    <div class="span1">
        <br/><a id="c1" href="#">Hello</a>
    </div>
    <div class="span1">
        <canvas id="myCanvas1" width="100" height="100"></canvas>
    </div>
    <div class="span1">
        <br/><a id="c2" href="#">Hi</a>
    </div>
</div>
</body>
</html>
于 2013-08-29T19:19:34.683 回答
0

为每个函数使用此代码脚本标记

<script>
  var canvas = document.getElementById('myCanvas');
  var context = canvas.getContext('2d');
  var centerX = canvas.width / 2;
  var centerY = canvas.height / 2;
  var radius = 70;

  context.beginPath();
  context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
  context.fillStyle = 'blue';
  context.fill();
  context.lineWidth = 5;
  context.strokeStyle = '#003300';
  context.stroke();
</script>
于 2013-08-29T07:02:49.443 回答