Example of how to use JavaScript + canvas to implement the seven color palette and canvas 7 color palette

Source: Internet
Author: User

Example of how to use JavaScript + canvas to implement the seven color palette and canvas 7 color palette

The example in this article describes how to implement a seven-color palette using JavaScript + canvas. We will share this with you for your reference. The details are as follows:

As follows:

Html:

<canvas id="canvas" class="canvas" width="600" height="600"></canvas>

Css:

html,body{margin: 0;padding: 0}.canvas{display: block; margin-left:auto;margin-right:auto;margin-top: 50px;}

Javascript:

var disk = [    {      area:[{x:0,y:0},{x:600,y:0},{x:300,y:300}],      color:"#CBF263"    },    {      area:[{x:0,y:0},{x:0,y:600},{x:300,y:300}],      color:"#5CB6D0"    },    {      area:[{x:0,y:600},{x:300,y:600},{x:150,y:450}],      color:"#FE9CCD"    },    {      area:[{x:150,y:450},{x:300,y:300},{x:450,y:450},{x:300,y:600}],      color:"#A696C3"    },    {      area:[{x:300,y:600},{x:600,y:600},{x:600,y:300}],      color:"#FBC421"    },    {      area:[{x:600,y:300},{x:600,y:0},{x:450,y:150},{x:450,y:450}],      color:"#FF5061"    },    {      area:[{x:450,y:450},{x:450,y:150},{x:300,y:300}],      color:"#FDEA11"    }]window.onload = function(){    var canvasDom = document.getElementById("canvas");    var ctx = canvasDom.getContext("2d");    drawDisk(disk,ctx)}function drawDisk(disk,ctx){    for(var i = 0;i<disk.length;i++){      ctx.beginPath();      ctx.moveTo(disk[i].area[0].x,disk[i].area[0].y);      for(var j = 1;j<disk[i]["area"].length;j++){        ctx.lineTo(disk[i].area[j].x,disk[i].area[j].y);      }      ctx.closePath();      ctx.fillStyle = disk[i]["color"];      ctx.fill();    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.