Javascript Canvas: 6Circles

1 minute read

The code has been modified slightly and now uploaded to the site.

Source is unknown.

<— Return Home

6Circles.html

<!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">
    <script src = "6Circles.js" defer></script>
    <title>Document</title>
</head>
<body>
    <h1>6Circles</h1>
    <canvas id = "myCanvas" width = "400" height = "400" style = "border-style: solid"></canvas>
</body>
</html>

<— Return Home

6Circles.js

let canvas = document.getElementById("myCanvas");
let context = canvas.getContext("2d");

context.save();
context.beginPath();
context.fillStyle = "green";
context.arc(canvas.width / 2, canvas.height / 2, 50, 0, 2 * Math.PI);
context.fill();
context.restore();

context.save();
context.beginPath();
context.translate(canvas.width / 2, canvas.height / 2);
let circle = new Circle("rgb(0, 0, 54)", 95, 40, 50, 0, 2 * Math.PI);
circle.draw();
context.restore();

context.save();
context.beginPath();
context.translate(canvas.width / 2, canvas.height / 2);
circle = new Circle("blue", 0, 100, 50, 0, 2 * Math.PI);
circle.draw();
context.restore();

context.save();
context.beginPath();
context.translate(canvas.width / 2, canvas.height / 2);
circle = new Circle("rgb(0, 0, 204)", -95, 40, 50, 0, 2 * Math.PI);
circle.draw();
context.restore();

context.save();
context.beginPath();
context.translate(canvas.width / 2, canvas.height / 2);
circle = new Circle("darkblue", -75, -70, 50, 0, 2 * Math.PI);
circle.draw();
context.restore();

context.save();
context.beginPath();
context.translate(canvas.width / 2, canvas.height / 2);
circle = new Circle("rgb(0, 1, 104)", 65, -79, 50, 0, 2 * Math.PI);
circle.draw();
context.restore();


function Circle(color, x, y, radius, startAngle, endAngle){
    this.color = color;
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.startAngle = startAngle;
    this.endAngle = endAngle;
    this.draw = function(){
        context.fillStyle = this.color;
        context.arc(x, y, radius, startAngle, endAngle);
        context.fill();
    }
}

// context.save();
// context.beginPath();
// context.fillStyle = "rgb(0, 0, 54)";
// context.translate(canvas.width / 2, canvas.height / 2);
// context.arc(95, 40, 50, 0, 2 * Math.PI);
// context.fill();
// context.restore();

// context.save();
// context.beginPath();
// context.fillStyle = "blue";
// context.translate(canvas.width / 2, canvas.height / 2);
// context.arc(0, 100, 50, 0, 2 * Math.PI);
// context.fill();

// context.save();

<— Return Home

Result

Open Link in New Tab

<— Return Home