本文介绍了如何旋转线性渐变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何让我的白线从底部矩形的点到点完美地形成对角线?
How can I make my white line go perfectly diagonal from point to point of my bottom rectangle?
https://jsfiddle.net/a7rs5qu5/
<canvas id="canvas" width="300" height="300"></canvas>
_canvas = document.getElementById('canvas');
_stage = _canvas.getContext('2d');
_stage.fillStyle = "#00FF00";
_stage.fillRect(0, 0, 300, 200);
var gradient = _stage.createLinearGradient(0, 200, 300, 300);
gradient.addColorStop(0, "blue");
gradient.addColorStop(.5, "white");
gradient.addColorStop(1, "blue");
_stage.fillStyle = gradient;
_stage.fillRect(0, 200, 300, 300);
推荐答案
通过数学,并改变梯度坐标.您需要设置梯度坐标,以便它们描述与其正交的直线.
By mathematics, and changing gradient coordinates. You need to set gradient coordinates so they describe a line orthogonal to it.
_canvas = document.getElementById('canvas');
_stage = _canvas.getContext('2d');
_stage.fillStyle = "#00FF00";
_stage.fillRect(0, 0, 300, 200);
var radius = 100;
var angle = Math.atan2(100, 300) + Math.PI / 2;
var gx = radius * Math.cos(angle);
var gy = radius * Math.sin(angle);
var cx = (0 + 300) / 2;
var cy = (200 + 300) / 2;
var gradient = _stage.createLinearGradient(cx - gx, cy - gy, cx + gx, cy + gy);
gradient.addColorStop(0, "blue");
gradient.addColorStop(.5, "white");
gradient.addColorStop(1, "blue");
_stage.fillStyle = gradient;
_stage.fillRect(0, 200, 300, 300);
<canvas id="canvas" width="300" height="300"></canvas>
radius
的选择控制渐变的宽度;上述值仅给出与上述代码中使用的值类似的值.
The selection of radius
controls how wide the gradient is; the above value merely gives values similar to the ones used in the code above.
这篇关于如何旋转线性渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!