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

bug修复 #216

Open
wants to merge 8 commits 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
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ node_modules
/src/generateDoc/doc
.vscode/chrome
/src/platform
build
/test/libs
/test/test
/test/screenshot.png
build
.idea
10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"scripts": {
"build": "cd src/publishTool&&cmd /k publish.bat",
"compile": "gulp LayaAirBuild",
"buildDoc": "cd src/generateDoc&&cmd /k run.bat"
"buildDoc": "cd src/generateDoc&&cmd /k run.bat",
"server": "http-server -p 8888 -c-1 -s --cors",
"pretest": "tsc -p src/casetest",
"test": "cd test && node test1.js"
},
"author": "Layabox Inc.",
"license": "SEE LICENSE IN LICENSE.md",
Expand All @@ -25,7 +28,10 @@
"typedoc": "^0.15.0",
"typescript": "^3.7.2"
},
"devDependencies": {},
"devDependencies": {
"looks-same": "^7.2.2",
"puppeteer-core": "^2.1.1"
},
"repository": {
"type": "git",
"url": "https://github.com/layabox/LayaAir"
Expand Down
36 changes: 20 additions & 16 deletions src/casetest/testWorkerLoader.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
import {delay} from './delay.js'
import { delay } from './delay.js'
import { Laya } from 'Laya.js';
import { Texture2D } from 'laya/resource/Texture2D.js';
import { Texture } from 'laya/resource/Texture.js';
import { WorkerLoader } from 'laya/net/WorkerLoader.js';
import { Sprite } from 'laya/display/Sprite.js';

class Main {
constructor() {
Laya.init(800,600);
//Laya.stage.scaleMode = 'fixedwidth';
Laya.stage.screenMode = 'none';
constructor() {
Laya.init(800, 600);
//Laya.stage.scaleMode = 'fixedwidth';
Laya.stage.screenMode = 'none';
//Laya.Stat.show();
this.test1();
}

/**
*/
async test1(){
var l = new WorkerLoader();
var url = 'http://127.0.0.1:8888/bin/res/monkey0.png';
l.on(url, this, function(d:Texture2D) {
var sp = new Sprite();
async test1() {
console.warn('请在火狐浏览器打开');
const workerLoader = new WorkerLoader();
const url = 'http://127.0.0.1:8888/bin/res/pixi/laser01.png';
workerLoader.on(url, this, function (bitmap: ImageBitmap) {
const texture2D = new Texture2D();
texture2D.loadImageSource(bitmap);// 火狐浏览器显示异常
// texture2D.loadImageSource(bitmap, true);// 正常
const sp = new Sprite();
Laya.stage.addChild(sp);
sp.graphics.drawTexture(new Texture(d));
} );
sp.graphics.drawTexture(new Texture(texture2D));
});

workerLoader.loadImage(url);

l.loadImage(url);

await delay(10); // 等待渲染结果
(window as any).testEnd=true; // 告诉测试程序可以停止了
(window as any).testEnd = true; // 告诉测试程序可以停止了
}
}

Expand Down
220 changes: 100 additions & 120 deletions src/layaAir/jsLibs/workerloader.js
Original file line number Diff line number Diff line change
@@ -1,132 +1,112 @@
var createImageBitmapOK=self.createImageBitmap?true:false;
onmessage =function (evt){

var data = evt.data;//通过evt.data获得发送来的数据
loadImage2(data);
if(!isSet)
{
isSet=true;
setInterval(workerloop,1000);
}
var createImageBitmapOK = self.createImageBitmap ? true : false;
onmessage = function (evt) {

var data = evt.data;//通过evt.data获得发送来的数据
loadImage2(data);
if (!isSet) {
isSet = true;
setInterval(workerloop, 1000);
}
}
var isSet=true;
function workerloop()
{
myTrace("png:workerloop");
var isSet = true;

function workerloop() {
myTrace("png:workerloop");
}

var enableTrace = false;
var ifShowTraceToMain = false;

var enableTrace=false;
var ifShowTraceToMain=false;
function myTrace(msg)
{
if(!enableTrace) return;
console.log("png:"+msg)
if(ifShowTraceToMain)
{
showMsgToMain(msg);
}
function myTrace(msg) {
if (!enableTrace) return;
console.log("png:" + msg)
if (ifShowTraceToMain) {
showMsgToMain(msg);
}
}
function loadImage2(url)
{
var xhr,
_this = this;
var failed=false;
xhr = new XMLHttpRequest;
xhr.open("GET", url, true);
//showMsgToMain("loadImage2");
xhr.responseType = "arraybuffer";
myTrace("load:"+url);
xhr.onload = function() {
var response=xhr.response || xhr.mozResponseArrayBuffer;
//showMsgToMain("onload:");
myTrace("onload:"+url);
if((xhr.status != 200&&xhr.status!=0)||response.byteLength<10)
{
if(!failed)
{
failed=true;
pngFail(url,"loadFail from onload"+xhr.status);
}

return;
}
var data, png;
data = new Uint8Array(response);
doCreateImageBitmap(data,url);

};
xhr.onerror = function(e){
pngFail(url,"loadFail");
}

xhr.send(null);
function loadImage2(url) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
//showMsgToMain("loadImage2");
xhr.responseType = "blob";
myTrace("load:" + url);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
//showMsgToMain("onload:");
myTrace("onload:" + url);
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
doCreateImageBitmap(xhr.response, url);
} else {
pngFail(url, "loadFail from onload" + xhr.status);
}
}
};

xhr.onerror = function (e) {
pngFail(url, "loadFail");
};

xhr.send(null);
}
function doCreateImageBitmap(response,url)
{
try
{
//showMsgToMain("hihidoCreateImageBitmap");
//showMsgToMain("doCreateImageBitmap:"+response);
//var startTime=getTimeNow();
//showMsgToMain("new self.Blob");
var startTime=getTimeNow();

response = new self.Blob([response],{type:"image/png"});
self.createImageBitmap(response).then(function(imageBitmap) {
//showMsgToMain("imageBitmapCreated:");
var data={};
data.url=url;
data.imageBitmap=imageBitmap;
data.dataType="imageBitmap";

data.startTime=startTime;
data.decodeTime=getTimeNow()-startTime;
data.sendTime=getTimeNow();

myTrace("png:Decode By createImageBitmap,"+data.decodeTime,url);

data.type="Image";
postMessage(data,[data.imageBitmap]);
}).catch(
function(e)
{
showMsgToMain("catch e:"+e);
pngFail(url,"parse fail"+e+":ya");
}
)
}catch(e)
{
pngFail(url,"parse fail"+e.toString()+":ya");
}

function doCreateImageBitmap(response, url) {
try {
//showMsgToMain("hihidoCreateImageBitmap");
//showMsgToMain("doCreateImageBitmap:"+response);
//var startTime=getTimeNow();
//showMsgToMain("new self.Blob");
var startTime = getTimeNow();
self.createImageBitmap(response).then(function (imageBitmap) {
//showMsgToMain("imageBitmapCreated:");
var data = {};
data.url = url;
data.imageBitmap = imageBitmap;
data.dataType = "imageBitmap";

data.startTime = startTime;
data.decodeTime = getTimeNow() - startTime;
data.sendTime = getTimeNow();

myTrace("png:Decode By createImageBitmap," + data.decodeTime, url);

data.type = "Image";
postMessage(data, [data.imageBitmap]);
}).catch(function (e) {
showMsgToMain("catch e:" + e);
pngFail(url, "parse fail" + e + ":ya");
});
} catch (e) {
pngFail(url, "parse fail" + e.toString() + ":ya");
}
}
function getTimeNow()
{
return new Date().getTime();

function getTimeNow() {
return new Date().getTime();
}
function disableWorker(msg)
{
var data={};
data.url=url;
data.imagedata=null;
data.type="Disable";
data.msg=msg;
postMessage(data);

function disableWorker(msg) {
var data = {};
data.url = url;
data.imagedata = null;
data.type = "Disable";
data.msg = msg;
postMessage(data);
}
function pngFail(url,msg)
{
var data={};
data.url=url;
data.imagedata=null;
data.type="Image";
data.msg=msg;
console.log("png:"+msg+" "+url);
postMessage(data);

function pngFail(url, msg) {
var data = {};
data.url = url;
data.imagedata = null;
data.type = "Image";
data.msg = msg;
console.log("png:" + msg + " " + url);
postMessage(data);
}
function showMsgToMain(msg)
{
var data={};
data.type="Msg";
data.msg=msg;
postMessage(data);

function showMsgToMain(msg) {
var data = {};
data.type = "Msg";
data.msg = msg;
postMessage(data);
}
2 changes: 1 addition & 1 deletion src/layaAir/laya/html/dom/HTMLDivElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class HTMLDivElement extends Sprite {
* @override
*/
destroy(destroyChild: boolean = true): void {
if (this._element) this._element.reset();
if (this._element) this._element.destroy();
this._element = null;
this._doClears();
super.destroy(destroyChild);
Expand Down
1 change: 0 additions & 1 deletion src/layaAir/laya/net/Loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ export class Loader extends EventDispatcher {
return;
}

Loader.setGroup(url, "666");
this._url = url;
if (url.indexOf("data:image") === 0 && !type) type = Loader.IMAGE;
else url = URL.formatURL(url);
Expand Down
2 changes: 1 addition & 1 deletion src/layaAir/laya/net/WorkerLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export class WorkerLoader extends EventDispatcher {
var image:any = imageData;
if (type !== "nativeimage") {
image = new Texture2D();
image.loadImageSource(imageData);
image.loadImageSource(imageData, true);
}

_this["onLoaded"](image);
Expand Down
Binary file added test/testResult/testWorkerLoader1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading