Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Shadow module #1507

Merged
merged 14 commits into from
Nov 2, 2020
1 change: 1 addition & 0 deletions src/Modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = {
'resize': require('./modules/Resize'),
'rotate': require('./modules/Rotate'),
'saturation': require('./modules/Saturation'),
'shadow': require('./modules/Shadow'),
'text-overlay': require('./modules/TextOverlay'),
'threshold': require('./modules/Threshold'),
'tint': require('./modules/Tint'),
Expand Down
146 changes: 146 additions & 0 deletions src/modules/Shadow/Module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Create Shadow
*/
module.exports = function canvasResize(options, UI) {

var defaults = require('./../../util/getDefaults.js')(require('./info.json'));
const pixelSetter = require('../../util/pixelSetter.js');

var output;

function draw(input, callback, progressObj) {

options.X_value = parseInt(options.X_value || defaults.X_value);
options.Y_value = parseInt(options.Y_value || defaults.Y_value);

progressObj.stop(true);
progressObj.overrideFlag = true;

var step = this;

function extraManipulation(pixels) {
let [w, h] = pixels.shape;
let newPixels = require('ndarray')(new Uint8Array(4 * (w + Math.abs(options.X_value)) * (h + Math.abs(options.Y_value))).fill(0), [(w + Math.abs(options.X_value)), (h + Math.abs(options.Y_value)), 4]);
let iMax = w,
jMax = h;
if (options.X_value < 0 && options.Y_value < 0) {
for (var k = 0; k < Math.abs(options.X_value); k++) {
for (var l = 0; l < (h + Math.abs(options.Y_value)); l++) {
let val = 255 - ((k / Math.abs(options.X_value)) * 255);
pixelSetter(k, l, [val, val, val, 255], newPixels);
}
}
for (var k = 0; k < (w + Math.abs(options.X_value)); k++) {
for (var l = 0; l < Math.abs(options.Y_value); l++) {
if (k < Math.abs(options.X_value) && k < l) {
continue;
}
let val = 255 - ((l / Math.abs(options.Y_value)) * 255);
pixelSetter(k, l, [val, val, val, 255], newPixels);
}
}
for (let i = 0; i < iMax && i < w; i++) {
for (let j = 0; j < jMax && j < h; j++) {
let x = i + Math.abs(options.X_value), y = j + Math.abs(options.Y_value);
pixelSetter(x, y, [pixels.get(i, j, 0), pixels.get(i, j, 1), pixels.get(i, j, 2), pixels.get(i, j, 3)], newPixels);
}
}
}

else if (options.X_value >= 0 && options.Y_value >= 0) {
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
for (var k = w; k < (w + options.X_value); k++) {
for (var l = 0; l < (h + options.Y_value); l++) {
let val = (((k - w) / options.X_value) * 255);
pixelSetter(k, l, [val, val, val, 255], newPixels);
}
}
for (var k = 0; k < (w + options.X_value); k++) {
for (var l = h; l < (h + options.Y_value); l++) {
if (k >= w && l >= h && ((k - w) >= (l - h))) {
continue;
}
let val = ((l - h) / options.Y_value * 255);
pixelSetter(k, l, [val, val, val, 255], newPixels);
}
}
for (let i = 0; i < iMax && i < w; i++) {
for (let j = 0; j < jMax && j < h; j++) {
let x = i, y = j;
pixelSetter(x, y, [pixels.get(i, j, 0), pixels.get(i, j, 1), pixels.get(i, j, 2), pixels.get(i, j, 3)], newPixels);
}
}
}

else if (options.X_value < 0 && options.Y_value >= 0) {
for (var k = 0; k < (w + Math.abs(options.X_value)); k++) {
for (var l = h; l < (h + options.Y_value); l++) {
let val = ((l - h) / options.Y_value * 255);
pixelSetter(k, l, [val, val, val, 255], newPixels);
}
}
for (var k = 0; k < Math.abs(options.X_value); k++) {
for (var l = 0; l < (h + options.Y_value); l++) {
if (l + (k * (options.Y_value / Math.abs(options.X_value))) - (options.Y_value + h) > 0 && l >= h) {
continue;
}
let val = 255 - ((k / Math.abs(options.X_value)) * 255);
pixelSetter(k, l, [val, val, val, 255], newPixels);
}
}
for (let i = 0; i < iMax && i < w; i++) {
for (let j = 0; j < jMax && j < h; j++) {
let x = i + Math.abs(options.X_value), y = j;
pixelSetter(x, y, [pixels.get(i, j, 0), pixels.get(i, j, 1), pixels.get(i, j, 2), pixels.get(i, j, 3)], newPixels);
}
}
}

else if (options.X_value >= 0 && options.Y_value < 0) {
for (var k = w; k < (w + options.X_value); k++) {
for (var l = 0; l < (h + Math.abs(options.Y_value)); l++) {
let val = (((k - w) / options.X_value) * 255);
pixelSetter(k, l, [val, val, val, 255], newPixels);
}
}
for (var k = 0; k < (w + options.X_value); k++) {
for (var l = 0; l < Math.abs(options.Y_value); l++) {
if (l >= ((options.X_value / Math.abs(options.Y_value)) * (w + options.X_value - k)) && k >= w) {
continue;
}
let val = 255 - (l / Math.abs(options.Y_value) * 255);
pixelSetter(k, l, [val, val, val, 255], newPixels);
}
}
for (let i = 0; i < iMax && i < w; i++) {
for (let j = 0; j < jMax && j < h; j++) {
let x = i, y = j + Math.abs(options.Y_value);
pixelSetter(x, y, [pixels.get(i, j, 0), pixels.get(i, j, 1), pixels.get(i, j, 2), pixels.get(i, j, 3)], newPixels);
}
}
}
return newPixels;
}

function output(image, datauri, mimetype, wasmSuccess) {
step.output = { src: datauri, format: mimetype, wasmSuccess, useWasm: options.useWasm };
}

return require('../_nomodule/PixelManipulation.js')(input, {
output: output,
ui: options.step.ui,
extraManipulation: extraManipulation,
format: input.format,
image: options.image,
inBrowser: options.inBrowser,
callback: callback,
useWasm:options.useWasm
});

}
return {
options: options,
draw: draw,
output: output,
UI: UI
};
};
4 changes: 4 additions & 0 deletions src/modules/Shadow/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = [
require('./Module'),
require('./info.json')
];
17 changes: 17 additions & 0 deletions src/modules/Shadow/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "shadow",
"description": "Creates a two sided shadow.",
"inputs": {
"X_value": {
"type": "integer",
"desc": "X-value",
"default": 20
},
"Y_value": {
"type": "integer",
"desc": "Y-value",
"default": 20
}
},
"docs-link":"https://github.com/publiclab/image-sequencer/blob/main/docs/MODULES.md#shadow-module"
niravasher marked this conversation as resolved.
Show resolved Hide resolved
}
8 changes: 8 additions & 0 deletions test/core/modules/shadow.js

Large diffs are not rendered by default.