Commit 4e3fe988 authored by wjf's avatar wjf

l

parent c04ff844
import {EventDispatcher} from "../events/EventDispatcher";
import {Parser} from "../svga/parser";
import {VideoEntity} from "../svga/VideoEntity";
import { EventDispatcher } from "../events/EventDispatcher";
import { Parser } from "../svga/parser";
import { VideoEntity } from "../svga/VideoEntity";
export class Loader extends EventDispatcher {
......@@ -31,16 +31,26 @@ export class Loader extends EventDispatcher {
let pngFile = url.substring(0, url.lastIndexOf('.')) + '.png';
this.loadImage((suc, data) => {
if (suc) {
this.cache(pngFile, data);
if (this.caches[url]) {
callback(true, {json: this.caches[url], img: data})
callback(true, { json: this.caches[url], img: data })
}
} else {
callback(false, data)
}
}, pngFile);
this.loadJson((suc, data) => {
if (suc) {
if (this.caches[pngFile]) {
callback(true, { json: data, img: this.caches[pngFile] })
}
} else {
callback(false, data)
}
}, url)
}
loadJson(callback: Function, url: string) {
//每次都要new
let _req;
if (window["XMLHttpRequest"]) {
......@@ -56,41 +66,28 @@ export class Loader extends EventDispatcher {
_req.send();
_req.onreadystatechange = () => {
if (_req.readyState == 4 && _req.status == 200) {
// console.log(s._req.responseText);
// var obj = JSON.parse(_req.responseText);
// console.log(obj)
this.cache(url, _req.response);
if (this.caches[pngFile]) {
callback(true, {json: _req.response, img: this.caches[pngFile]})
}
callback(true, _req.response)
}
};
_req.onerror = (reason): void => {
callback(false, reason)
}
}
}
//暂时先不用fetch。safari兼容问题
// fetchAsync(url)
// .then((data) => {
// this.cache(url, data);
// if (this.caches[pngFile]) {
// callback(true, { json: data, img: this.caches[pngFile] })
// }
// // console.log(data)
// })
// .catch(reason => {
// callback(false, reason)
// // console.log(reason.message)
// })
loadTexture(url: string) {
this.loadImage((s, img) => { if (s) this.cache(url, img) }, url)
}
loadImage(callback: Function, url: string, crossOrigin: boolean = true) {
let self = this
let img = new Image();
if (crossOrigin) {
img.setAttribute('crossOrigin', 'anonymous');
}
img.onload = function (e) {
self.cache(url, img);
callback(true, img);
};
img.onerror = function (e) {
......
import { Loader } from "../2d/loader/Loader";
import { Texture } from "../2d/texture";
const loader = new Loader;
const caches={}
export function loadTexture(url: string) {
loader.loadImage(() => {
}, url,)
}
//loadText和loadJson
export function getRes() {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment