Implement HTMLCanvasElement rendering

This commit is contained in:
MoyuScript
2017-08-03 20:57:55 +08:00
parent 12232dfe5a
commit 2fde30f429
4 changed files with 31 additions and 12 deletions

View File

@@ -1,11 +1,11 @@
/* @flow */
'use strict';
import type NodeContainer from './NodeContainer';
import type Options from './index';
import type Logger from './Logger';
type ImageCache = {[string]: Promise<Image>};
export type ImageElement = Image | HTMLCanvasElement;
type ImageCache = {[string]: Promise<ImageElement>};
export default class ImageLoader {
origin: string;
@@ -13,12 +13,14 @@ export default class ImageLoader {
_link: HTMLAnchorElement;
cache: ImageCache;
logger: Logger;
_index: number;
constructor(options: Options, logger: Logger) {
this.options = options;
this.origin = this.getOrigin(window.location.href);
this.cache = {};
this.logger = logger;
this._index = 0;
}
loadImage(src: string): ?string {
@@ -33,6 +35,12 @@ export default class ImageLoader {
}
}
loadCanvas(node: HTMLCanvasElement): string {
const key = String(this._index++);
this.cache[key] = Promise.resolve(node);
return key;
}
isInlineImage(src: string): boolean {
return /data:image\/.*;base64,/i.test(src);
}
@@ -86,14 +94,14 @@ export default class ImageLoader {
export class ImageStore {
_keys: Array<string>;
_images: Array<HTMLImageElement>;
_images: Array<ImageElement>;
constructor(keys: Array<string>, images: Array<HTMLImageElement>) {
constructor(keys: Array<string>, images: Array<ImageElement>) {
this._keys = keys;
this._images = images;
}
get(key: string): ?HTMLImageElement {
get(key: string): ?ImageElement {
const index = this._keys.indexOf(key);
return index === -1 ? null : this._images[index];
}