<legend id='MWpq1'><style id='MWpq1'><dir id='MWpq1'><q id='MWpq1'></q></dir></style></legend>
<i id='MWpq1'><tr id='MWpq1'><dt id='MWpq1'><q id='MWpq1'><span id='MWpq1'><b id='MWpq1'><form id='MWpq1'><ins id='MWpq1'></ins><ul id='MWpq1'></ul><sub id='MWpq1'></sub></form><legend id='MWpq1'></legend><bdo id='MWpq1'><pre id='MWpq1'><center id='MWpq1'></center></pre></bdo></b><th id='MWpq1'></th></span></q></dt></tr></i><div id='MWpq1'><tfoot id='MWpq1'></tfoot><dl id='MWpq1'><fieldset id='MWpq1'></fieldset></dl></div>
<tfoot id='MWpq1'></tfoot>
    • <bdo id='MWpq1'></bdo><ul id='MWpq1'></ul>

    1. <small id='MWpq1'></small><noframes id='MWpq1'>

      1. HTML Canvas 渐变只显示一种颜色

        时间:2023-08-02

          <legend id='mSzNf'><style id='mSzNf'><dir id='mSzNf'><q id='mSzNf'></q></dir></style></legend>

              <bdo id='mSzNf'></bdo><ul id='mSzNf'></ul>

              <small id='mSzNf'></small><noframes id='mSzNf'>

              • <i id='mSzNf'><tr id='mSzNf'><dt id='mSzNf'><q id='mSzNf'><span id='mSzNf'><b id='mSzNf'><form id='mSzNf'><ins id='mSzNf'></ins><ul id='mSzNf'></ul><sub id='mSzNf'></sub></form><legend id='mSzNf'></legend><bdo id='mSzNf'><pre id='mSzNf'><center id='mSzNf'></center></pre></bdo></b><th id='mSzNf'></th></span></q></dt></tr></i><div id='mSzNf'><tfoot id='mSzNf'></tfoot><dl id='mSzNf'><fieldset id='mSzNf'></fieldset></dl></div>
                    <tbody id='mSzNf'></tbody>

                • <tfoot id='mSzNf'></tfoot>
                • 本文介绍了HTML Canvas 渐变只显示一种颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在使用 Canvas gradient 时遇到问题,它只显示我在 gradient.__addColorStop__(offset,color) 方法上设置的最后一种颜色.

                  I'm having problems with Canvas gradient it only shows the last color that I set on gradient.__addColorStop__(offset,color) method.

                  例如,这是我更好理解的一段代码:

                  For example here is a piece of my code to understand better:

                  let canvas = document.getElementById('canvas');
                  let ctx = canvas.getContext('2d');
                  let gradient = ctx.createLinearGradient(10, 90, 200, 90);
                  gradient.addColorStop(1 / 10, "black");
                  gradient.addColorStop(1 / 5, "yellow");
                  gradient.addColorStop(1 / 1, "red");
                  let circle1 = new Circle(300, 250, 50, gradient);
                  circle1.draw(ctx);
                  

                  该代码仅绘制一个红色圆圈,并对填充渐变的任何形状执行相同操作,如果我评论此行 gradient.addColorStop(1/1,"red");然后画布绘制一个黄色圆圈,只读取最后一个颜色.我在 jsfiddle.net 上尝试了相同的代码并且运行良好,但我不知道为什么我的脚本无法运行.

                  That code only draw a red circle and do the same with any shape filled with the gradient, if i comment this line gradient.addColorStop(1/1,"red");then canvas draw a yellow circle, only read the last color. I tried the same code on jsfiddle.net and works perfectly, i dont know why my script wont work.

                  PD:Circle 是我定义的一个 js 对象,可以完美运行

                  PD: Circle is a js object that i defined and works perfectly

                  对不起我的英语,如果帖子听不懂请告诉我,这是我在 StackOverflow 上的第一篇帖子.谢谢!

                  Sorry for my english, if the post doesn't understand please tell me, is my first post on StackOverflow. Thanks!

                  推荐答案

                  CanvasGradients 是相对于上下文的转换矩阵,而不是你要填充它的形状.

                  CanvasGradients are relative to the context's transformation matrix, not to the shape you'll fill it with.

                  因此,在您的示例中,由于您正在绘制水平渐变,因此您只能在从 x:10 到 x:200 的区域中设置此渐变.x:10 之前的像素将具有索引 0 处的值,x:200 之后的像素将具有索引 1 处的值.
                  由于您在 300,150 处绘制半径为 50 的圆,因此您的圆将达到的最小 x 位置为 250,这在您的渐变索引:1 之后,因此为纯红色.

                  So in your example, since you are drawing an horizontal gradient, you set this gradient only in an area that goes from x:10 to x:200. Pixels before x:10 will have the value at index 0, and the ones after x:200 the one at index 1.
                  Since you are drawing your circle at 300,150 with a radius of 50, the minimal x position your circle will attain is 250, which is after your gradient index:1 and hence solid red.

                  这里是发生了什么的视觉演示:

                  Here is a visual demo of what happens:

                  let canvas = document.getElementById('canvas');
                  let ctx = canvas.getContext('2d');
                  let gradient = ctx.createLinearGradient(10, 90, 200, 90);
                  gradient.addColorStop(1 / 10, "black");
                  gradient.addColorStop(1 / 5, "yellow");
                  gradient.addColorStop(1 / 1, "red");
                  
                  // draw a full rectangle to see how the gradient is actually rendered
                  ctx.fillStyle = gradient;
                  ctx.fillRect(0,0,canvas.width,canvas.height);
                  
                  ctx.beginPath();
                  ctx.arc(300, 150, 50, 0, Math.PI*2);
                  ctx.strokeStyle = 'white';
                  ctx.stroke();

                  <canvas id="canvas" width="500" height="300"></canvas>

                  要避免这种情况,您有两种方法:

                  To circumvent this, you have two ways:

                  • 在正确的坐标处生成 CanvasGradient:

                  let canvas = document.getElementById('canvas');
                  let ctx = canvas.getContext('2d');
                  // x1 & x2 are set to match the circle's position
                  let gradient = ctx.createLinearGradient(250, 90, 350, 90);
                  gradient.addColorStop(1 / 10, "black");
                  gradient.addColorStop(1 / 5, "yellow");
                  gradient.addColorStop(1 / 1, "red");
                  
                  ctx.fillStyle = gradient;
                  
                  ctx.beginPath();
                  ctx.arc(300, 150, 50, 0, Math.PI*2);
                  ctx.fill();

                  <canvas id="canvas" width="500" height="300"></canvas>

                  • 修改上下文的转换矩阵以移动 CanvasGradient:

                  let canvas = document.getElementById('canvas');
                  let ctx = canvas.getContext('2d');
                  let gradient = ctx.createLinearGradient(10, 90, 200, 90);
                  gradient.addColorStop(1 / 10, "black");
                  gradient.addColorStop(1 / 5, "yellow");
                  gradient.addColorStop(1 / 1, "red");
                  
                  ctx.fillStyle = gradient;
                  
                  ctx.beginPath();
                  ctx.arc(300, 150, 50, 0, Math.PI*2);
                  // our arc has been defined at the correct position
                  // we can now translate the context matrix so that only the fillStyle be moved
                  ctx.translate(230, 0);
                  ctx.fill();
                  
                  // reset the default tranform matrix
                  ctx.setTransform(1,0,0,1,0,0);

                  <canvas id="canvas" width="500" height="300"></canvas>

                  这篇关于HTML Canvas 渐变只显示一种颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何检查鼠标单击是否在 JavaScript 中 HTML5 Canvas 上的旋转文本内? 下一篇:如何检查两个绘图(线)是否在 Canvas 中碰撞?

                  相关文章

                  1. <tfoot id='p2NKd'></tfoot>

                    <small id='p2NKd'></small><noframes id='p2NKd'>

                      • <bdo id='p2NKd'></bdo><ul id='p2NKd'></ul>

                    1. <legend id='p2NKd'><style id='p2NKd'><dir id='p2NKd'><q id='p2NKd'></q></dir></style></legend>
                      <i id='p2NKd'><tr id='p2NKd'><dt id='p2NKd'><q id='p2NKd'><span id='p2NKd'><b id='p2NKd'><form id='p2NKd'><ins id='p2NKd'></ins><ul id='p2NKd'></ul><sub id='p2NKd'></sub></form><legend id='p2NKd'></legend><bdo id='p2NKd'><pre id='p2NKd'><center id='p2NKd'></center></pre></bdo></b><th id='p2NKd'></th></span></q></dt></tr></i><div id='p2NKd'><tfoot id='p2NKd'></tfoot><dl id='p2NKd'><fieldset id='p2NKd'></fieldset></dl></div>