Skip to content
This repository has been archived by the owner on Sep 29, 2020. It is now read-only.

Feature/scale watermark #57

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"presets": ["es2015"]
"presets": ["@babel/preset-env"]
}
1 change: 1 addition & 0 deletions __mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'test-file-stub';
12 changes: 12 additions & 0 deletions config/jest/cssTransform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This is a custom Jest transformer turning style imports into empty objects.
// http://facebook.github.io/jest/docs/tutorial-webpack.html

module.exports = {
process() {
return 'module.exports = {};';
},
getCacheKey() {
// The output is always the same.
return 'cssTransform';
},
};
10 changes: 10 additions & 0 deletions config/jest/fileTransformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path');

// This is a custom Jest transformer turning file imports into filenames.
// http://facebook.github.io/jest/docs/tutorial-webpack.html

module.exports = {
process(src, filename) {
return `module.exports = ${JSON.stringify(path.basename(filename))};`;
},
};
1,153 changes: 212 additions & 941 deletions dist/watermark.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/watermark.min.js

Large diffs are not rendered by default.

1,153 changes: 212 additions & 941 deletions examples/scripts/watermark.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/scripts/watermark.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require('babel-register');
require('@babel/register');
module.exports = require('./lib/index').default;
26 changes: 26 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
name: 'Jazida Testes Unitários/Integração',
notify: true,
verbose: true,
testEnvironment: 'node',
testURL: 'http://localhost:3000/',
setupFiles: ['<rootDir>/lib/setup.js'],
testMatch: [
'<rootDir>/**/__tests__/**/*-(spec|test).js?(x)',
'<rootDir>/**/?(*.)(spec|test).js?(x)',
],
collectCoverageFrom: ['/**/*.{js,jsx}'],
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$'],
modulePathIgnorePatterns: ['<rootDir>/node_modules'],
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/__mocks__/fileMock.js',
'\\.(css|sass)$': 'identity-obj-proxy',
},
transform: {
'^.+\\.js$': 'babel-jest',
'^.+\\.css$': '<rootDir>/config/jest/cssTransform.js',
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/config/jest/fileTransformer.js',
},
};
26 changes: 13 additions & 13 deletions lib/__tests__/watermark-test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import watermark from '../';

const blob = require('../blob');
blob.blob = jest.genMockFn();
blob.blob = jest.fn()

const canvas = require('../canvas');
canvas.dataUrl = jest.genMockFn();
canvas.dataUrl = jest.fn()

const image = require('../image');
image.load = jest.genMockFn();
image.mapToCanvas = jest.genMockFn();
image.imageToCanvas = jest.genMockFn();
image.createImage = jest.genMockFn();
image.load = jest.fn()
image.mapToCanvas = jest.fn()
image.imageToCanvas = jest.fn()
image.createImage = jest.fn()

const style = require('../style');
style.result = jest.genMockFn();
style.result = jest.fn()

describe('watermark', function () {

Expand All @@ -29,7 +29,7 @@ describe('watermark', function () {
});

it('can load urls and files', function () {
const urls = ['url1', new File()];
const urls = ['url1', new File(["foo"], "foo.txt")];
const init = () => console.log(initialized);

watermark(urls, {
Expand All @@ -41,7 +41,7 @@ describe('watermark', function () {

describe('.dataUrl()', function () {
it('returns a new object structure', function () {
const draw = jest.genMockFunction();
const draw = jest.fn();
const first = watermark(['url1', 'url2'], {}, promise);
first.then = () => first;
const second = first.dataUrl(draw);
Expand All @@ -53,9 +53,9 @@ describe('watermark', function () {
describe('.blob()', function () {
it('should delegate to the dataUrl function and map to a blob', function () {
const mark = watermark(['url1', 'url1'], {}, promise);
mark.dataUrl = jest.genMockFunction();
mark.dataUrl = jest.fn();
mark.dataUrl.mockReturnValueOnce(promise);
const draw = jest.genMockFunction();
const draw = jest.fn();

const newMark = mark.blob(draw);

Expand All @@ -68,9 +68,9 @@ describe('watermark', function () {
describe('.image()', function () {
it('should delegate to the dataUrl function and map to an image', function () {
const mark = watermark(['url1', 'url1'], {}, promise);
mark.dataUrl = jest.genMockFunction();
mark.dataUrl = jest.fn();
mark.dataUrl.mockReturnValueOnce(promise);
const draw = jest.genMockFunction();
const draw = jest.fn();

const newMark = mark.image(draw);

Expand Down
5 changes: 3 additions & 2 deletions lib/blob/__tests__/blob-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {split, decode, uint8} from '../';

const testUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJYAAACWCAYAAAA8AXHiAAAgAElEQ…z4wYdQblhRRGYSxWfKk909djpT+VOeAue/QVI/7VHKn/3/ANXZ6cTaQP0eAAAAAElFTkSuQmCC';
const testUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=';

describe('blobs', function () {
it('can break a data url into parts', function () {
Expand All @@ -16,7 +16,8 @@ describe('blobs', function () {
});

it('can convert raw data to a Uint8Array', function () {
const decoded = decode('hello');
const encoded = window.btoa('hello');
const decoded = decode(encoded);
const ints = uint8(decoded);
expect(ints[0]).toEqual(decoded.charCodeAt(0));
});
Expand Down
2 changes: 1 addition & 1 deletion lib/canvas/__tests__/pool-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CanvasPool } from '../pool';
*/
const releasable = canvas => {
const context = {
clearRect: jest.genMockFn()
clearRect: jest.fn()
};
canvas.getContext = twoD => {
canvas.context = context;
Expand Down
7 changes: 4 additions & 3 deletions lib/image/__tests__/image-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {createImage, getLoader} from '../';

const baseURL = 'http://localhost:3000/';

describe('image', function () {
it('can create a new image', function () {
const onload = () => console.log('loaded');
const img = createImage('url1', onload);
expect(img.src).toBe('url1');
const img = createImage(`${baseURL}url1`, onload);
expect(img.src).toBe(`${baseURL}url1`);
expect(img.onload).toBe(onload);
});

Expand Down
21 changes: 21 additions & 0 deletions lib/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { JSDOM } from 'jsdom';

const jsdom = new JSDOM('<!doctype html><html><body></body></html>');
const { window } = jsdom;

function copyProps(src, target) {
const props = Object.getOwnPropertyNames(src)
.filter(prop => typeof target[prop] === 'undefined')
.reduce((result, prop) => ({
...result,
[prop]: Object.getOwnPropertyDescriptor(src, prop),
}), {});
Object.defineProperties(target, props);
}

global.window = window;
global.document = window.document;
global.navigator = {
userAgent: 'node.js',
};
copyProps(window, global);
19 changes: 17 additions & 2 deletions lib/style/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,29 @@
* @param {Number} alpha
* @return {Function}
*/
export function atPos(xFn, yFn, alpha) {
export function atPos(xFn, yFn, alpha, scale = {}) {
alpha || (alpha = 1.0);
return function (target, watermark) {
const context = target.getContext('2d');
const width = (target, watermark) => {
if (scale.width) {
return scale.width(target, watermark);
}

return watermark.width;
}

const height = (target, watermark) => {
if (scale.height) {
return scale.height(target, watermark);
}

return watermark.height;
}
context.save();

context.globalAlpha = alpha;
context.drawImage(watermark, xFn(target, watermark), yFn(target, watermark));
context.drawImage(watermark, xFn(target, watermark), yFn(target, watermark), width(target, watermark), height(target, watermark));

context.restore();
return target;
Expand Down
Loading