@vibrant/image-browser
Advanced tools
Comparing version 3.0.0 to 3.1.0-0
@@ -1,5 +0,3 @@ | ||
/// <reference types="bluebird" /> | ||
import * as Bluebird from 'bluebird'; | ||
import { ImageData as VibrantImageData, ImageSource, ImageBase } from '@vibrant/image'; | ||
export default class BroswerImage extends ImageBase { | ||
export default class BrowserImage extends ImageBase { | ||
image: HTMLImageElement; | ||
@@ -10,4 +8,4 @@ private _canvas; | ||
private _height; | ||
private _initCanvas(); | ||
load(image: ImageSource): Bluebird<ImageBase>; | ||
private _initCanvas; | ||
load(image: ImageSource): Promise<ImageBase>; | ||
clear(): void; | ||
@@ -14,0 +12,0 @@ update(imageData: VibrantImageData): void; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
return function (d, b) { | ||
@@ -13,28 +16,29 @@ extendStatics(d, b); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Bluebird = require("bluebird"); | ||
var image_1 = require("@vibrant/image"); | ||
var Url = require("url"); | ||
function isRelativeUrl(url) { | ||
var u = Url.parse(url); | ||
return u.protocol === null | ||
&& u.host === null | ||
&& u.port === null; | ||
var u = new URL(url, location.href); | ||
return u.protocol === location.protocol && | ||
u.host === location.host && | ||
u.port === location.port; | ||
} | ||
function isSameOrigin(a, b) { | ||
var ua = Url.parse(a); | ||
var ub = Url.parse(b); | ||
var ua = new URL(a); | ||
var ub = new URL(b); | ||
// https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy | ||
return ua.protocol === ub.protocol | ||
&& ua.hostname === ub.hostname | ||
&& ua.port === ub.port; | ||
return (ua.protocol === ub.protocol && | ||
ua.hostname === ub.hostname && | ||
ua.port === ub.port); | ||
} | ||
var BroswerImage = /** @class */ (function (_super) { | ||
__extends(BroswerImage, _super); | ||
function BroswerImage() { | ||
var BrowserImage = /** @class */ (function (_super) { | ||
__extends(BrowserImage, _super); | ||
function BrowserImage() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
BroswerImage.prototype._initCanvas = function () { | ||
BrowserImage.prototype._initCanvas = function () { | ||
var img = this.image; | ||
var canvas = this._canvas = document.createElement('canvas'); | ||
var context = this._context = canvas.getContext('2d'); | ||
var canvas = (this._canvas = document.createElement('canvas')); | ||
var context = (canvas.getContext('2d')); | ||
if (!context) | ||
throw new ReferenceError('Failed to create canvas context'); | ||
this._context = context; | ||
canvas.className = '@vibrant/canvas'; | ||
@@ -47,9 +51,13 @@ canvas.style.display = 'none'; | ||
}; | ||
BroswerImage.prototype.load = function (image) { | ||
BrowserImage.prototype.load = function (image) { | ||
var _this = this; | ||
var img = null; | ||
var src = null; | ||
var img; | ||
var src; | ||
if (typeof image === 'string') { | ||
img = document.createElement('img'); | ||
src = image; | ||
if (!isRelativeUrl(src) && !isSameOrigin(window.location.href, src)) { | ||
img.crossOrigin = 'anonymous'; | ||
} | ||
img.src = src; | ||
} | ||
@@ -61,12 +69,6 @@ else if (image instanceof HTMLImageElement) { | ||
else { | ||
return Bluebird.reject(new Error("Cannot load buffer as an image in browser")); | ||
return Promise.reject(new Error("Cannot load buffer as an image in browser")); | ||
} | ||
this.image = img; | ||
if (!isRelativeUrl(src) && !isSameOrigin(window.location.href, src)) { | ||
img.crossOrigin = 'anonymous'; | ||
} | ||
if (typeof image === 'string') { | ||
img.src = src; | ||
} | ||
return new Bluebird(function (resolve, reject) { | ||
return new Promise(function (resolve, reject) { | ||
var onImageLoad = function () { | ||
@@ -86,15 +88,15 @@ _this._initCanvas(); | ||
}; | ||
BroswerImage.prototype.clear = function () { | ||
BrowserImage.prototype.clear = function () { | ||
this._context.clearRect(0, 0, this._width, this._height); | ||
}; | ||
BroswerImage.prototype.update = function (imageData) { | ||
BrowserImage.prototype.update = function (imageData) { | ||
this._context.putImageData(imageData, 0, 0); | ||
}; | ||
BroswerImage.prototype.getWidth = function () { | ||
BrowserImage.prototype.getWidth = function () { | ||
return this._width; | ||
}; | ||
BroswerImage.prototype.getHeight = function () { | ||
BrowserImage.prototype.getHeight = function () { | ||
return this._height; | ||
}; | ||
BroswerImage.prototype.resize = function (targetWidth, targetHeight, ratio) { | ||
BrowserImage.prototype.resize = function (targetWidth, targetHeight, ratio) { | ||
var _a = this, canvas = _a._canvas, context = _a._context, img = _a.image; | ||
@@ -106,14 +108,16 @@ this._width = canvas.width = targetWidth; | ||
}; | ||
BroswerImage.prototype.getPixelCount = function () { | ||
BrowserImage.prototype.getPixelCount = function () { | ||
return this._width * this._height; | ||
}; | ||
BroswerImage.prototype.getImageData = function () { | ||
BrowserImage.prototype.getImageData = function () { | ||
return this._context.getImageData(0, 0, this._width, this._height); | ||
}; | ||
BroswerImage.prototype.remove = function () { | ||
this._canvas.parentNode.removeChild(this._canvas); | ||
BrowserImage.prototype.remove = function () { | ||
if (this._canvas && this._canvas.parentNode) { | ||
this._canvas.parentNode.removeChild(this._canvas); | ||
} | ||
}; | ||
return BroswerImage; | ||
return BrowserImage; | ||
}(image_1.ImageBase)); | ||
exports.default = BroswerImage; | ||
exports.default = BrowserImage; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@vibrant/image-browser", | ||
"version": "3.0.0", | ||
"version": "3.1.0-0", | ||
"description": "Browser vibrant ImageClass implementation", | ||
"scripts": { | ||
"build:module": "tsc", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
@@ -11,12 +10,17 @@ }, | ||
"types": "lib/index.d.ts", | ||
"author": "", | ||
"license": "ISC", | ||
"author": { | ||
"name": "akfish", | ||
"email": "akfish@gmail.com" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/akfish/node-vibrant/issues" | ||
}, | ||
"homepage": "https://github.com/akfish/node-vibrant", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@vibrant/image": "^3.0.0", | ||
"bluebird": "^3.5.1" | ||
"@vibrant/image": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/bluebird": "^3.5.18", | ||
"@types/node": "^8.0.53", | ||
"typescript": "latest" | ||
"@types/node": "^10.12.18", | ||
"typescript": "^3.2.2" | ||
}, | ||
@@ -23,0 +27,0 @@ "publishConfig": { |
194
src/index.ts
@@ -1,110 +0,122 @@ | ||
import * as Bluebird from 'bluebird' | ||
import { ImageOptions, ImageData as VibrantImageData, ImageSource, ImageCallback, ImageBase } from '@vibrant/image' | ||
import * as Url from 'url' | ||
import { | ||
ImageOptions, | ||
ImageData as VibrantImageData, | ||
ImageSource, | ||
ImageCallback, | ||
ImageBase | ||
} from '@vibrant/image' | ||
function isRelativeUrl(url: string): boolean { | ||
let u = Url.parse(url) | ||
return u.protocol === null | ||
&& u.host === null | ||
&& u.port === null | ||
function isRelativeUrl (url: string): boolean { | ||
let u = new URL(url, location.href) | ||
return u.protocol === location.protocol && | ||
u.host === location.host && | ||
u.port === location.port | ||
} | ||
function isSameOrigin(a: string, b: string): boolean { | ||
let ua = Url.parse(a) | ||
let ub = Url.parse(b) | ||
function isSameOrigin (a: string, b: string): boolean { | ||
let ua = new URL(a) | ||
let ub = new URL(b) | ||
// https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy | ||
return ua.protocol === ub.protocol | ||
&& ua.hostname === ub.hostname | ||
&& ua.port === ub.port | ||
// https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy | ||
return ( | ||
ua.protocol === ub.protocol && | ||
ua.hostname === ub.hostname && | ||
ua.port === ub.port | ||
) | ||
} | ||
export default class BroswerImage extends ImageBase { | ||
image: HTMLImageElement | ||
private _canvas: HTMLCanvasElement | ||
private _context: CanvasRenderingContext2D | ||
private _width: number | ||
private _height: number | ||
private _initCanvas(): void { | ||
let img = this.image | ||
let canvas = this._canvas = document.createElement('canvas') | ||
let context = this._context = canvas.getContext('2d') | ||
export default class BrowserImage extends ImageBase { | ||
image: HTMLImageElement | ||
private _canvas: HTMLCanvasElement | ||
private _context: CanvasRenderingContext2D | ||
private _width: number | ||
private _height: number | ||
private _initCanvas (): void { | ||
const img = this.image | ||
const canvas = (this._canvas = document.createElement('canvas')) | ||
const context = (canvas.getContext('2d')) | ||
canvas.className = '@vibrant/canvas' | ||
canvas.style.display = 'none' | ||
if (!context) throw new ReferenceError('Failed to create canvas context') | ||
this._width = canvas.width = img.width | ||
this._height = canvas.height = img.height | ||
this._context = context | ||
context.drawImage(img, 0, 0) | ||
canvas.className = '@vibrant/canvas' | ||
canvas.style.display = 'none' | ||
document.body.appendChild(canvas) | ||
} | ||
load(image: ImageSource): Bluebird<ImageBase> { | ||
let img: HTMLImageElement = null | ||
let src: string = null | ||
if (typeof image === 'string') { | ||
img = document.createElement('img') | ||
src = image | ||
} else if (image instanceof HTMLImageElement) { | ||
img = image | ||
src = image.src | ||
} else { | ||
return Bluebird.reject(new Error(`Cannot load buffer as an image in browser`)) | ||
} | ||
this.image = img | ||
this._width = canvas.width = img.width | ||
this._height = canvas.height = img.height | ||
if (!isRelativeUrl(src) && !isSameOrigin(window.location.href, src)) { | ||
img.crossOrigin = 'anonymous' | ||
} | ||
context.drawImage(img, 0, 0) | ||
if (typeof image === 'string') { | ||
img.src = src | ||
} | ||
document.body.appendChild(canvas) | ||
} | ||
load (image: ImageSource): Promise<ImageBase> { | ||
let img: HTMLImageElement | ||
let src: string | ||
if (typeof image === 'string') { | ||
img = document.createElement('img') | ||
src = image | ||
return new Bluebird<ImageBase>((resolve, reject) => { | ||
let onImageLoad = () => { | ||
this._initCanvas() | ||
resolve(this) | ||
} | ||
if (!isRelativeUrl(src) && !isSameOrigin(window.location.href, src)) { | ||
img.crossOrigin = 'anonymous' | ||
} | ||
if (img.complete) { | ||
// Already loaded | ||
onImageLoad() | ||
} else { | ||
img.onload = onImageLoad | ||
img.onerror = (e) => reject(new Error(`Fail to load image: ${src}`)) | ||
} | ||
}) | ||
img.src = src | ||
} else if (image instanceof HTMLImageElement) { | ||
img = image | ||
src = image.src | ||
} else { | ||
return Promise.reject( | ||
new Error(`Cannot load buffer as an image in browser`) | ||
) | ||
} | ||
clear(): void { | ||
this._context.clearRect(0, 0, this._width, this._height) | ||
} | ||
update(imageData: VibrantImageData): void { | ||
this._context.putImageData(<ImageData>imageData, 0, 0) | ||
} | ||
getWidth(): number { | ||
return this._width | ||
} | ||
getHeight(): number { | ||
return this._height | ||
} | ||
resize(targetWidth: number, targetHeight: number, ratio: number): void { | ||
let { _canvas: canvas, _context: context, image: img } = this | ||
this.image = img | ||
this._width = canvas.width = targetWidth | ||
this._height = canvas.height = targetHeight | ||
return new Promise<ImageBase>((resolve, reject) => { | ||
let onImageLoad = () => { | ||
this._initCanvas() | ||
resolve(this) | ||
} | ||
context.scale(ratio, ratio) | ||
context.drawImage(img, 0, 0) | ||
if (img.complete) { | ||
// Already loaded | ||
onImageLoad() | ||
} else { | ||
img.onload = onImageLoad | ||
img.onerror = e => reject(new Error(`Fail to load image: ${src}`)) | ||
} | ||
}) | ||
} | ||
clear (): void { | ||
this._context.clearRect(0, 0, this._width, this._height) | ||
} | ||
update (imageData: VibrantImageData): void { | ||
this._context.putImageData(imageData as ImageData, 0, 0) | ||
} | ||
getWidth (): number { | ||
return this._width | ||
} | ||
getHeight (): number { | ||
return this._height | ||
} | ||
resize (targetWidth: number, targetHeight: number, ratio: number): void { | ||
let { _canvas: canvas, _context: context, image: img } = this | ||
this._width = canvas.width = targetWidth | ||
this._height = canvas.height = targetHeight | ||
context.scale(ratio, ratio) | ||
context.drawImage(img, 0, 0) | ||
} | ||
getPixelCount (): number { | ||
return this._width * this._height | ||
} | ||
getImageData (): ImageData { | ||
return this._context.getImageData(0, 0, this._width, this._height) | ||
} | ||
remove (): void { | ||
if (this._canvas && this._canvas.parentNode) { | ||
this._canvas.parentNode.removeChild(this._canvas) | ||
} | ||
getPixelCount(): number { | ||
return this._width * this._height | ||
} | ||
getImageData(): ImageData { | ||
return this._context.getImageData(0, 0, this._width, this._height) | ||
} | ||
remove(): void { | ||
this._canvas.parentNode.removeChild(this._canvas) | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
16326
1
2
292
0
8
1
- Removedbluebird@^3.5.1