Generate a Buddhist avatar with canvas

Continue to create, accelerate growth! This is the 5th day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the event details

When I was writing every day, I accidentally saw my head portrait of Nuggets. A bunch of circles were arranged irregularly, which seemed unusually Buddhist. I’m free, or I’ll show you guys to achieve this effect. By the way, I will review canvas.

image.png

draw a circle

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <canvas id="canvas" width="300" height="200"></canvas>
</body>
<script>
    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');
    ctx.beginPath();
    ctx.arc(100, 100, 100, 0, 2 * Math.PI, false);
    ctx.closePath();
    ctx.fillStyle = 'blue';
    ctx.fill();
</script>

</html>
复制代码

c0d682bbdf508165ad6c2c3a0704b40.png

package it

drawCircle('blue', 100, 100, 100, 0, 2 * Math.PI);
function drawCircle(color, x, y, r, start, end, anticlockwise = false) {
    ctx.beginPath();
    ctx.arc(x, y, r, start, end, anticlockwise);
    ctx.closePath();
    ctx.fillStyle = color;
    ctx.fill();
}
复制代码

Parameter description: color: Color, x: Coordinate of the center of the circle x, y: The y coordinate of the center of the circle, r: The size of the radius, start: The starting angle, end: The ending angle, anticlockwise: Whether it is counterclockwise or not

draw more circles

for (let i = 1; i <= 10; i++) {
    drawCircle('blue', 20 * i, 10 * i, 10, 0, 2 * Math.PI)
}
复制代码

1497d7a5693e5b97ddee5e9309c2821.png

random color

Randomly generate a number, you can read an article I wrote before. Randomly generate 6 digits, how would you write it?

function getRandomColor() {
    return '#' + Math.random().toString(16).slice(2, 8);
}
for (let i = 1; i <= 10; i++) {
    drawCircle(getRandomColor(), 20 * i, 10 * i, 10, 0, 2 * Math.PI)
}
复制代码

832833d8bbe316c720a6b26e8660834.png

random location

function getRandomPositionX() {
    return Math.floor(Math.random() * 300);
}
function getRandomPositionY() {
    return Math.floor(Math.random() * 200);
}
for (let i = 1; i <= 10; i++) {
    drawCircle(getRandomColor(), getRandomPositionX(), getRandomPositionY(), 10, 0, 2 * Math.PI)
}
复制代码

4fdd598afdf2ea8ebfb9fa4854a0112.png

random size

function getRandomSize() {
    return 3 + Math.floor(Math.random() * 17);
}
for (let i = 1; i <= 10; i++) {
    drawCircle(getRandomColor(), getRandomPositionX(), getRandomPositionY(), getRandomSize(), 0, 2 * Math.PI)
}
复制代码

4627598262ec7813f420d6151b1f02b.png

add animation

function animate() {
    requestAnimationFrame(animate);
    drawCircle(getRandomColor(), getRandomPositionX(), getRandomPositionY(), getRandomSize(), 0, 2 * Math.PI)
}
animate();
复制代码

11222.gif

Stop after 100 are generated

let num = 0;
function animate() {
    if(num === 100) return;
    num++;
    requestAnimationFrame(animate);
    drawCircle(getRandomColor(), getRandomPositionX(), getRandomPositionY(), getRandomSize(), 0, 2 * Math.PI)
}
animate();
复制代码

112222.gif

random transparency

It is not easy to distinguish the balls when they are stacked together, and the transparency will be better:

function getRandomNum() {
    return Math.floor(Math.random() * 256);
}
function getRandomColorWithTransparent() {
    return `rgba(${getRandomNum()}, ${getRandomNum()}, ${getRandomNum()}, ${Math.random()})`;
}
let num = 0;
function animate() {
    if (num === 100) return;
    num++;
    requestAnimationFrame(animate);
    drawCircle(getRandomColorWithTransparent(), getRandomPositionX(), getRandomPositionY(), getRandomSize(), 0, 2 * Math.PI)
}
animate();
复制代码

11222222.gif

Code on the Nuggets

Everyone can knock out their own ideas right away and create more interesting effects.

Guess you like

Origin juejin.im/post/7103079778789359624