简体   繁体   English

在Windows Paint等JavaScript中将颜色填充到HTML画布的特定部分

[英]Filling the color to specific section of HTML canvas in JavaScript like Windows Paint

I am now trying to deeply understand the canvas and JavaScript. 我现在正在尝试深入了解画布和JavaScript。 Now I am drawing an image on HTML canvas using JavaScript. 现在,我正在使用JavaScript在HTML画布上绘制图像。 I can draw the image successfully. 我可以成功绘制图像。 But I am having a problem with filling the colour. 但是我在填充颜色方面遇到问题。 This is the picture I have drawn using JavaScript and canvas. 这是我使用JavaScript和canvas绘制的图片。

在此处输入图片说明

This is the JavaScript code. 这是JavaScript代码。

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

    //circles
    var center_y = 150;//Y
    var center_x = 150;
    var lineWidth = 10;
    context.lineWidth=lineWidth;

    context.beginPath();
    context.arc(center_x,center_y ,50,0,2*Math.PI);
    context.strokeStyle = "#FF00FF";
    context.stroke();

    context.beginPath();
    context.arc(center_x,center_y ,40,0,2*Math.PI);
    context.strokeStyle = "#990000";
    context.stroke();

    context.beginPath();
    context.arc(center_x,center_y ,30,0,2*Math.PI);
    context.strokeStyle = "#0099CC";
    context.stroke();


    context.lineWidth = 1;
    x1 = 150;
    y1 = 150;
    r =  140;
    theta = 0.5;
    context.moveTo(x1, y1);
    context.lineTo(x1 + r * Math.cos(theta), y1 + r * Math.sin(theta));
    context.stroke();

    theta = 1;
    context.moveTo(x1, y1);
    context.lineTo(x1 + r * Math.cos(theta), y1 + r * Math.sin(theta));
    context.stroke();

    theta = 1.5;
    context.moveTo(x1, y1);
    context.lineTo(x1 + r * Math.cos(theta), y1 + r * Math.sin(theta));
    context.stroke();

    theta = 2;
    context.moveTo(x1, y1);
    context.lineTo(x1 + r * Math.cos(theta), y1 + r * Math.sin(theta));
    context.stroke();

    theta = 2.5;
    context.moveTo(x1, y1);
    context.lineTo(x1 + r * Math.cos(theta), y1 + r * Math.sin(theta));
    context.stroke();


    theta = 3;
    context.moveTo(x1, y1);
    context.lineTo(x1 + r * Math.cos(theta), y1 + r * Math.sin(theta));
    context.stroke();

Basically, what I am doing is drawing circles from smaller to bigger with the same center. 基本上,我正在做的是在同一中心从小到大绘制圆。 Then draw the line from the same center increasing the degree equally after each line is drawn. 然后在绘制每条线之后,从同一中心绘制线,以均等的程度增加其度数。 Yes, the image is drawn successfully. 是的,图像绘制成功。 But there is an issue with what I want to achieve. 但是我要实现的目标存在问题。 As you can see, for coloring the circle, I set the line with of circle to ten and set the color for the line. 如您所见,为了给圆上色,我将带有圆的线设置为十,并设置了线的颜色。 So the whole circle line has only one color. 因此,整个圆线只有一种颜色。 But what I would like to do is I want to set the different color for each section of the circle. 但是我想做的是我想为圆的每个部分设置不同的颜色。 I set the section by separating with the lines as follow. 我通过如下分隔线来设置部分。

在此处输入图片说明

So, what I want to do is, I want to set the different color to section A of circle "1" from section B of circle "1" and so on. 所以,我想做的是,我要为圆“ 1”的B部分中的圆“ 1”的A部分设置不同的颜色,依此类推。 In Microsoft Paint software, if we paint a section, if the section is bordered properly, only the bordered area section is painted. 在Microsoft画图软件中,如果我们绘制一个部分,如果该部分正确边框,则仅绘制边框区域。 So, I would like to do something like that. 所以,我想做这样的事情。

You should probably draw different arcs for the different sections by changing the last 2 parameters of the arc method: 您可能应该通过更改arc方法的最后两个参数为不同的部分绘制不同的弧:

context.arc(cx,cy ,radius, theta_start, theta_end);

I made this jsfiddle for you: https://jsfiddle.net/gtato/znxzvjnp/ 我为您制作了这个jsfiddle: https ://jsfiddle.net/gtato/znxzvjnp/

If you want to set to set the different color to section A of circle "1" from section B of circle "1" and so on, you can use draw arcs next to each other. 如果要设置为将不同的颜色设置为与圆“ 1”的B区域不同的圆“ 1”的A区域,以此类推,则可以将绘制弧彼此相邻使用。 You have to calculate the angle from and to and make sure these are adjacent. 您必须计算与之间的夹角,并确保它们相邻。

Here is a small example on how you can draw something like a piechart with random colors: 这是一个有关如何绘制具有随机颜色的类似饼图的小示例:

 const canvas = document.getElementById('canvas'); const context = canvas.getContext('2d'); context.lineWidth = 1; x1 = 150; y1 = 150; r = 150; let prevAngle = 0; let angle = 0; let fraction = 0.05; for (i = 0; i * fraction <= 1; i++) { context.fillStyle = getRandomColor(); // Calculate new angle from previous one angle = prevAngle + fraction * Math.PI * 2; //create a path context.beginPath(); context.moveTo(x1, y1); context.arc(x1, y1, r, prevAngle, angle, false); context.lineTo(x1, y1); //fill it context.fill(); // Create a stroke context.strokeStyle = "#000000"; context.stroke(); prevAngle = angle; } function getRandomColor() { var letters = '0123456789ABCDEF'; var color = '#'; for (var i = 0; i < 6; i++) { color += letters[Math.floor(Math.random() * 16)]; } return color; } 
 <canvas width="500" height="500" id="canvas"> </canvas> 

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

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