This repository has been archived on 2022-01-28. You can view files and clone it, but cannot push or open issues or pull requests.
beebot/filters.js

38 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-02-25 18:47:47 +01:00
const _ = require('lodash');
const Canvas = require('canvas');
const { _drawImage } = require('./imageex');
const logger = require('loglevel');
logger.setLevel(process.env.LOGLEVEL || 'info');
2018-02-25 18:47:47 +01:00
const { createCanvas, Image } = Canvas;
const filters = {
pride: (canvas, source, x, y, props) => {
_.each(canvas.frames, frame => {
2018-02-26 13:23:54 +01:00
const basicProps = { width: props.width, height: props.height };
2018-02-25 18:47:47 +01:00
const tmpCanvas = createCanvas(frame.canvas.width, frame.canvas.height);
const tmpCtx = tmpCanvas.getContext('2d');
2018-02-26 13:23:54 +01:00
_drawImage(tmpCtx, frame.canvas, x, y, basicProps);
2018-02-25 18:47:47 +01:00
const multiplyProps = _.extend({}, props, { attributes: { globalCompositeOperation: 'multiply' } });
logger.debug('multiply props:', multiplyProps);
2018-02-25 18:47:47 +01:00
_drawImage(tmpCtx, source.frames[0].canvas, x, y, multiplyProps);
const tmpCanvas2 = createCanvas(frame.canvas.width, frame.canvas.height);
const tmpCtx2 = tmpCanvas2.getContext('2d');
2018-02-26 13:23:54 +01:00
_drawImage(tmpCtx2, frame.canvas, x, y, basicProps);
2018-02-25 18:47:47 +01:00
const sourceInProps = _.extend({}, props, { attributes: { globalCompositeOperation: 'source-in' } });
_drawImage(tmpCtx2, source.frames[0].canvas, x, y, sourceInProps);
2018-02-26 13:23:54 +01:00
const combineProps2 = _.extend({}, basicProps, { attributes: { globalCompositeOperation: 'source-atop', globalAlpha: 1 } });
2018-02-25 18:47:47 +01:00
_drawImage(frame.ctx, tmpCanvas2, x, y, combineProps2);
2018-02-26 13:23:54 +01:00
const combineProps = _.extend({}, basicProps, { attributes: { globalCompositeOperation: 'source-atop', globalAlpha: 0.6 } });
2018-02-25 18:47:47 +01:00
_drawImage(frame.ctx, tmpCanvas, x, y, combineProps);
});
return canvas;
}
};
module.exports = filters;