html5 随机 圆 不重叠,使用HTML5 Canvas和分形算法生成不完美圆形

这篇博客介绍了如何使用JavaScript和Canvas API创建动态图形。通过Babel和CoffeeScript的转换,实现窗口加载事件监听,然后在Canvas上进行图形绘制。文章详细展示了如何检查Canvas支持,初始化画布,设置线条点,以及生成和绘制圆圈。此外,还包含了一个防止鼠标点击事件影响浏览器窗口的处理函数。
摘要由CSDN通过智能技术生成

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

window.addEventListener("load", windowLoadHandler, false);

//for debug messages

var Debugger = function() { };

Debugger.log = function(message) {

try {

console.log(message);

}

catch (exception) {

return;

}

}

function windowLoadHandler() {

canvasApp();

}

function canvasSupport() {

return Modernizr.canvas;

}

function canvasApp() {

if (!canvasSupport()) {

return;

}

var theCanvas = document.getElementById("canvasOne");

var context = theCanvas.getContext("2d");

var displayWidth = theCanvas.width;

var displayHeight = theCanvas.height;

init();

function init() {

generate();

theCanvas.addEventListener("click", clickListener, false);

}

function clickListener(evt) {

context.clearRect(0,0,theCanvas.width,theCanvas.height);

generate();

//code below prevents the mouse down from having an effect on the main browser window:

if (evt.preventDefault) {

evt.preventDefault();

} //standard

else if (evt.returnValue) {

evt.returnValue = false;

} //older IE

return false;

}

function setLinePoints(iterations) {

var pointList = {};

pointList.first = {x:0, y:1};

var lastPoint = {x:1, y:1}

var minY = 1;

var maxY = 1;

var point;

var nextPoint;

var dx, newX, newY;

pointList.first.next = lastPoint;

for (var i = 0; i < iterations; i++) {

point = pointList.first;

while (point.next != null) {

nextPoint = point.next;

dx = nextPoint.x - point.x;

newX = 0.5*(point.x + nextPoint.x);

newY = 0.5*(point.y + nextPoint.y);

newY += dx*(Math.random()*2 - 1);

var newPoint = {x:newX, y:newY};

//min, max

if (newY < minY) {

minY = newY;

}

else if (newY > maxY) {

maxY = newY;

}

//put between points

newPoint.next = nextPoint;

point.next = newPoint;

point = nextPoint;

}

}

//normalize to values between 0 and 1

if (maxY != minY) {

var normalizeRate = 1/(maxY - minY);

point = pointList.first;

while (point != null) {

point.y = normalizeRate*(point.y - minY);

point = point.next;

}

}

//unlikely that max = min, but could happen if using zero iterations. In this case, set all points equal to 1.

else {

point = pointList.first;

while (point != null) {

point.y = 1;

point = point.next;

}

}

return pointList;

}

function generate() {

var centerX, centerY;

var r,g,b,a;

var color;

var lineW;

var maxRad, minRad;

var phase;

for (var i = 0; i < 20; i++) {

maxRad = 50+Math.random()*50;;

minRad = 0.88*maxRad;

centerX = maxRad + Math.random()*(displayWidth-2*maxRad);

centerY = maxRad + Math.random()*(displayHeight-2*maxRad);

r = Math.floor(Math.random()*255);

g = Math.floor(Math.random()*255);

b = Math.floor(Math.random()*255);

a = 0.4;

color = "rgba("+r+","+g+","+b+","+a+")";

phase = Math.random()*Math.PI*2;

drawCircle(centerX, centerY, minRad, maxRad, phase, color);

}

}

function drawCircle(centerX, centerY, minRad, maxRad, phase, color) {

var point;

var rad, theta;

var twoPi = 2*Math.PI;

var x0,y0;

//generate the random function that will be used to vary the radius, 9 iterations of subdivision

var pointList = setLinePoints(9);

context.strokeStyle = color;

context.lineWidth = 1.01;

context.fillStyle = color;

context.beginPath();

point = pointList.first;

theta = phase;

rad = minRad + point.y*(maxRad - minRad);

x0 = centerX + rad*Math.cos(theta);

y0 = centerY + rad*Math.sin(theta);

context.lineTo(x0, y0);

while (point.next != null) {

point = point.next;

theta = twoPi*point.x + phase;

rad = minRad + point.y*(maxRad - minRad);

x0 = centerX + rad*Math.cos(theta);

y0 = centerY + rad*Math.sin(theta);

context.lineTo(x0, y0);

}

context.stroke();

context.fill();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值