簡體   English   中英

使用html5單擊對應鏈接時如何用顏色填充圓圈

[英]How to fill a circle with color when clicked on a correspong link using html5

我已經嘗試了這么多的代碼。 我旁邊有兩個圓圈,其中一個鏈接,我需要為相應鏈接的onclick圓圈上色。 提出一些解決方案

<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>

單擊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>

對每個功能使用此代碼腳本標記

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM