简体   繁体   中英

Operate on part of an image using CamanJS

I'm using Caman JS to modify an image. I want to modify only the alpha channel of a subset of pixels. but when i apply the filter it modifies all the pixels in the image. Any suggestions on how to just modify the subset of pixels. Do i need to create a plugin instead of a filter?

window.doWhatever=->
   offset={x:170,y:150}
   #for i in [0..10]
   #  for j in [0..10]
   #    x=j+offset.x
   #    y=i+offset.y
   #    image.maskAlpha(x,y,0)
   x=50
   y=50
   image.maskAlpha(x,y,255)
   image.render()
 Caman.Filter.register 'maskAlpha',(x,y,alphain)  ->
   this.process 'maskAlpha', ->
   pixel=this.getPixel(x,y)
   this.putPixel(x,y,{
   r:pixel.r,
   g:pixel.g,
   b:pixel.b,
   a:alphain})

or in javascript
window.doWhatever = function() { var offset, x, y; offset = { x: 170, y: 150 }; x = 50; y = 50; image.maskAlpha(x, y, 255); return image.render(); };

 Caman.Filter.register('maskAlpha', function(x, y, alphain) {
   return this.process('maskAlpha', function() {
     var pixel;
     pixel = this.getPixel(x, y);
     return this.putPixel(x, y, {
       r: pixel.r,
       g: pixel.g,
       b: pixel.b,
       a: alphain
      });
   });
 });

There isn't a clean way to do this due to the defect in webkit and other browser implementation of canvas where it overwrites the RGB values with zero if alpha is set to 0.

alpha is over written question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM