Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to apply filters with code?(html/canvas)

Community Beginner ,
Feb 05, 2018 Feb 05, 2018

Below is the code I'm using straight out of createJs docs and I can't seem to get filters to work when I apply them with code. Any help? thanks

var matrix = new createjs.ColorMatrix().adjustHue(180).adjustSaturation(100);

root.myMc.filters = [

     new createjs.ColorMatrixFilter(matrix)

];

501
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 05, 2018 Feb 05, 2018

This took some working out!

Filters only work with objects that are cached, but it turns that you can cache them after applying the filters. Try this:

var mc = this.myMc;

var matrix = new createjs.ColorMatrix().adjustHue(180).adjustSaturation(100);

mc.filters = [

     new createjs.ColorMatrixFilter(matrix)

];

mc.cache(0,0,136,121);

The 136 and 121 just happen to be the width and height of the movieclip I tested on. I don't think there is such a thing as 'root', so I changed it to 'this'.

Translate
LEGEND ,
Feb 05, 2018 Feb 05, 2018

This took some working out!

Filters only work with objects that are cached, but it turns that you can cache them after applying the filters. Try this:

var mc = this.myMc;

var matrix = new createjs.ColorMatrix().adjustHue(180).adjustSaturation(100);

mc.filters = [

     new createjs.ColorMatrixFilter(matrix)

];

mc.cache(0,0,136,121);

The 136 and 121 just happen to be the width and height of the movieclip I tested on. I don't think there is such a thing as 'root', so I changed it to 'this'.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 06, 2018 Feb 06, 2018

Thank you that was it. I was missing the cache()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 06, 2018 Feb 06, 2018
LATEST

If you'd like to set the cache dimensions automatically, you can use the nominalBounds property, which is calculated at publish time and thus guaranteed to be accurate (less any runtime scaling).

var nb = clip.nominalBounds;

clip.filters = [ new createjs.ColorFilter(0.5, 0.5, 0.5, 1, 128, 0, 0, 0) ];

clip.cache(nb.x, nb.y, nb.width, nb.height);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines