Injecting a new stroke color into a shape

做~自己de王妃 提交于 2019-12-13 22:21:57

问题


I have a drawing canvas (based on the EaselJS CurveTo example) and when on mouseup I'd like to inject a new color into the shape being rendered to the canvas. I'd also like to do it without using a filter.

BIN: https://jsbin.com/qejesuvovu/1/edit?html,output

I used to directly injecting a custom fill/stroke into the shape graphics, but in later versions of EaselJS this has stopped working.

marker.graphics._dirty = true;
marker.graphics._fill = new createjs.Graphics.Fill('blue');
marker.graphics._stroke = new createjs.Graphics.Stroke('blue');

What is the recommended way of changing the color of a shape with a single stroke color on the fly?

UPDATE:

After looking into this further it turns out I need to be able to maintain the ability clone a shape and also preserve functionality for changing the color independently.

BIN: https://jsbin.com/gorasizuho/edit?html,output


回答1:


In version 0.7.0, Graphics were rearchitected to use "commands". The API for drawing is no different (except for the removal of the old appendInstruction API), however you can modify the properties of individual commands any time, and they will be reflected when the stage is updated.

var shape = new createjs.Shape();
shape.graphics.setStrokeStyle(3);
var strokeCommand = shape.graphics.beginStroke("#f00").command;
shape.graphics.drawRect(0,0,100,100);

You can modify attributes of any command:

strokeCommand.style = "#00f";

You can see a full list of commands in the Graphics docs. http://createjs.com/docs/easeljs/classes/Graphics.html#property_command - each command has individual documentation: http://createjs.com/docs/easeljs/classes/Graphics.Stroke.html

Here is an example using commands to modify both stroke-dash offset and drawRect coordinates: http://jsfiddle.net/lannymcnie/gg8sv4cq/

You can read more here: http://blog.createjs.com/new-command-approach-to-easeljs-graphics/



来源:https://stackoverflow.com/questions/33534887/injecting-a-new-stroke-color-into-a-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!