简体   繁体   中英

Draw dots on html5 canvas

I have a percentage that I want to represent as dots on a canvas. This is essentially a 10 x 10 matrix where I have a dot or image that represents one percent. so when it is 100% it will be green and when it is 10% only 10 of the dots will be green and the rest red.

Any idea what would be the best way to approach this problem?

something similar to this:

http://www.mathgoodies.com/lessons/vol4/images/percent_grid2.gif

Except they should be circles/images instead of squares?

Here i created a simple example. Hope it'd be helpful to you.

  var canvas = document.getElementById('mycanvas'); var context = canvas.getContext('2d'); var sizeX = canvas.width / 10; var sizeY = canvas.height / 10; var total = 15; var count = 0; for (var j = 0; j < 10; j++) { for (var i = 0; i < 10; i++) { context.beginPath(); context.arc(sizeX * (i+.5), sizeY * (j+.5), sizeX / Math.PI, 0, 2 * Math.PI, false); context.fillStyle = total > count ? 'green' : 'red'; context.fill(); count++; } } 
 <canvas id="mycanvas" width="200" height="200" style="border:1px solid black;"></canvas> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM