简体   繁体   English

在html5画布上绘制点

[英]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. 这本质上是一个10 x 10的矩阵,其中的点或图像代表百分之一。 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. 因此当它为100%时将为绿色,而当它为10%时将只有10个点为绿色,其余的为红色。

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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