Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

javascript - Creating Rainbow Gradient CreateJS

****Edit*** I have figured out the issue my next question is, why is that if I change the first X point and leave the others at 0 that I get my desired gradient effect?

enter image description here

function init(){
    var canvas = document.getElementById("easel"),
    SIZE = 250,
    centerX = canvas.width/2,
    centerY = canvas.height/2;

////////////////////////////////////
///////// Rainbow Arc /////////////
//////////////////////////////////  
    var newStroke = new createjs.Shape();
    //newStroke.graphics.beginStroke("#000");
    newStroke.graphics.beginLinearGradientStroke(["#ff0000","#ff6600","#ffff00","#009933","#0033cc","#4b0082","#551a8b"], [0,.14,.28,.42,.56,.70,.84,.98], 190,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
    newStroke.graphics.setStrokeStyle(20, 1, 1);
    newStroke.graphics.arc(100,100,50, 0 ,180*(Math.PI/180), true);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will have to create one manually. An alternative could be to render this to an off-screen canvas, then use that canvas as an image with CreateJS.

A quick way to do create a cone gradient:

var ctx = c.getContext("2d"),
    radius1 = 110,                                  // inner radius
    radius2 = 150,                                  // outer radius
    gap = Math.ceil(radius2 * 2 * Math.PI / 360);   // gap between each degree

for(var a = 360; a--;) {
  ctx.setTransform(1,0,0,1,150,150);                // 150=center (x,y)
  ctx.rotate(a / 180 * Math.PI);                    // current angle
  ctx.fillStyle = "hsl(" + a + ",100%,50%)";        // color using HSL based on angle
  ctx.fillRect(radius1, 0, radius2 - radius1, gap); // fill a segment
}
<canvas id=c height=300></canvas>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...