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

Add typescript support to the library #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 59 additions & 54 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,61 @@
{
"name": "merge-images",
"version": "2.0.0",
"description": "Easily compose images together without messing around with canvas",
"main": "dist/index.umd.js",
"module": "dist/index.es2015.js",
"files": [
"dist"
],
"scripts": {
"prebuild": "rm -rf dist",
"build": "rollup -c",
"pretest": "npm run build",
"test": "nyc ava",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"prelint": "npm run build",
"lint": "xo",
"prepublish": "npm run build"
},
"xo": {
"env": "browser",
"extends": "xo-lukechilds"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lukechilds/merge-images.git"
},
"keywords": [
"compose",
"merge",
"images",
"without",
"no",
"canvas"
],
"author": "Luke Childs <[email protected]> (http://lukechilds.co.uk)",
"license": "MIT",
"bugs": {
"url": "https://github.com/lukechilds/merge-images/issues"
},
"homepage": "https://github.com/lukechilds/merge-images",
"dependencies": {},
"devDependencies": {
"ava": "^0.25.0",
"camelcase": "^5.0.0",
"canvas": "^2.6.1",
"coveralls": "^3.0.0",
"datauri": "^1.0.5",
"eslint-config-xo-lukechilds": "^1.0.0",
"nyc": "^11.0.2",
"pify": "^3.0.0",
"rollup": "^0.59.0",
"rollup-plugin-buble": "^0.19.2",
"xo": "^0.18.2"
}
"name": "merge-images",
"version": "2.0.0",
"description": "Easily compose images together without messing around with canvas",
"main": "dist/index.js",
"module": "dist/index.es.js",
"browser": "dist/index.bundle.js",
"files": [
"dist"
],
"scripts": {
"prebuild": "rm -rf dist",
"build": "rollup -c && tsc",
"pretest": "npm run build",
"test": "nyc ava",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"prelint": "npm run build",
"lint": "xo",
"prepublish": "npm run build"
},
"xo": {
"env": "browser",
"extends": "xo-lukechilds"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lukechilds/merge-images.git"
},
"keywords": [
"compose",
"merge",
"images",
"without",
"no",
"canvas"
],
"author": "Luke Childs <[email protected]> (http://lukechilds.co.uk)",
"license": "MIT",
"bugs": {
"url": "https://github.com/lukechilds/merge-images/issues"
},
"homepage": "https://github.com/lukechilds/merge-images",
"dependencies": {},
"devDependencies": {
"ava": "^0.25.0",
"browser-env": "^3.3.0",
"camelcase": "^5.0.0",
"canvas": "^2.6.1",
"coveralls": "^3.0.0",
"datauri": "^1.0.5",
"eslint-config-xo-lukechilds": "^1.0.0",
"nyc": "^11.0.2",
"pify": "^3.0.0",
"rollup": "^0.68.0",
"rollup-plugin-terser": "^4.0.4",
"rollup-plugin-typescript2": "^0.19.3",
"tslib": "^2.4.1",
"typescript": "^4.9.3",
"xo": "^0.18.2"
}
}
47 changes: 27 additions & 20 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import buble from 'rollup-plugin-buble';
import camelCase from 'camelcase';
import typescript from 'rollup-plugin-typescript2';
import pkg from './package.json';
import { terser } from 'rollup-plugin-terser';

const pkg = require('./package.json');
require('fs').unlink('dist/index.d.ts', (err) => {});

export default {
input: 'src/index.js',
plugins: [
buble()
],
output: [
{
file: pkg.main,
format: 'umd',
name: camelCase(pkg.name),
sourcemap: true
},
{
file: pkg.module,
format: 'es',
sourcemap: true
}
]
input: 'src/index.ts',
output: [
{
file: pkg.main,
format: 'cjs'
},
{
file: pkg.module,
format: 'es'
},
{
file: pkg.browser,
format: 'iife',
name: 'TextSelect'
}
],
external: [...Object.keys(pkg.dependencies || {})],
plugins: [
typescript({
typescript: require('typescript')
}),
terser()
]
};
72 changes: 0 additions & 72 deletions src/index.js

This file was deleted.

101 changes: 101 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { MergeImageOptions, MergeImageSource } from './utils/types';
import { Image as ImageType } from 'canvas';

// Defaults
const defaultOptions: MergeImageOptions = {
format: 'image/png',
quality: 0.92,
width: undefined,
height: undefined,
Canvas: undefined,
crossOrigin: undefined
};

// Return Promise
const mergeImages = (sources: Array<string | MergeImageSource> = [], options: MergeImageOptions = {}) =>
new Promise<string>((resolve) => {
options = Object.assign({}, defaultOptions, options);

// Setup browser/Node.js specific variables
const canvas = options.Canvas ? new options.Canvas(1, 1, 'image') : window.document.createElement('canvas');
const Image = options.Image || window.Image;

// Load sources
const images = sources.map(
(source) =>
new Promise<{
img: ImageType | HTMLImageElement;
src?: string;
opacity?: number;
x?: number;
y?: number;
}>((resolve, reject) => {
// Convert string sources to objects
if (typeof source === 'string') {
source = { src: source };
}

// Resolve source and img when loaded
const img = new Image();

if (img instanceof HTMLImageElement && options.crossOrigin) {
img.crossOrigin = options.crossOrigin;
}

img.onerror = () => reject(new Error("Couldn't load image"));
img.onload = () => resolve(Object.assign({}, source, { img }));
img.src = source.src;
})
);

// Get canvas context
const ctx = canvas.getContext('2d');

// When sources have loaded
resolve(
Promise.all(images).then((images) => {
// Set canvas dimensions
const getSize = (dim: 'width' | 'height') =>
options[dim] || Math.max(...images.map((image) => image.img[dim]));
canvas.width = getSize('width');
canvas.height = getSize('height');

if (ctx instanceof CanvasRenderingContext2D) {
// Draw images to canvas
images.forEach((image) => {
ctx.globalAlpha = image.opacity ? image.opacity : 1;
if (image.img instanceof HTMLImageElement) {
/** @TODO Need add a fixer for type error in Node Js */
return ctx.drawImage(image.img, image.x || 0, image.y || 0);
}
});
}

if (options.Canvas && options.format === 'image/jpeg') {
// Resolve data URI for node-canvas jpeg async
return new Promise<string>((resolve, reject) => {
canvas.toDataURL(
'image/jpeg',
{
quality: options.quality,
progressive: false
},
(err, jpeg) => {
if (err) {
reject(err);
return;
}
resolve(jpeg);
}
);
});
}

// Resolve all other data URIs sync
return canvas.toDataURL(options.format, options.quality);
})
);
});

export default mergeImages;
export { MergeImageOptions, MergeImageSource };
18 changes: 18 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Canvas, Image } from "canvas";

export type MergeImageOptions = {
format?: string;
quality?: number;
width?: number;
height?: number;
Canvas?: typeof Canvas | undefined;
Image?: typeof Image | undefined;
crossOrigin?: "anonymous" | "use-credentials" | undefined;
};

export type MergeImageSource = {
src: string;
opacity?: number;
x?: number;
y?: number;
};
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"declaration": true,
"declarationDir": "./dist",
"noImplicitAny": true,
"outDir": "./dist",
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["dist", "node_modules"]
}