Skip to content

Commit

Permalink
Merge pull request #1 from trinketapp/feat/async-draw-and-setup
Browse files Browse the repository at this point in the history
Feat/async draw and setup
  • Loading branch information
albertjan authored Jan 5, 2018
2 parents 4cc86a9 + 301bfd7 commit 56085c6
Show file tree
Hide file tree
Showing 3 changed files with 768 additions and 785 deletions.
72 changes: 40 additions & 32 deletions processing.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module.exports = {
// Platform IDs
OTHER: 0,
WINDOWS: 1,
MAXOSX: 2,
MACOSX: 2,
LINUX: 3,

EPSILON: 0.0001,
Expand Down Expand Up @@ -9844,7 +9844,6 @@ module.exports = function setupParser(Processing, options) {
loopStarted = false,
renderSmooth = false,
doLoop = true,
looping = 0,
curRectMode = PConstants.CORNER,
curEllipseMode = PConstants.CENTER,
normalX = 0,
Expand Down Expand Up @@ -13603,13 +13602,14 @@ module.exports = function setupParser(Processing, options) {
p.pmouseY = pmouseYLastFrame;

saveContext();
p.draw();
restoreContext();
return p.draw().then(function() {
restoreContext();

pmouseXLastFrame = p.mouseX;
pmouseYLastFrame = p.mouseY;
p.pmouseX = pmouseXLastEvent;
p.pmouseY = pmouseYLastEvent;
pmouseXLastFrame = p.mouseX;
pmouseYLastFrame = p.mouseY;
p.pmouseX = pmouseXLastEvent;
p.pmouseY = pmouseYLastEvent;
});
};

Drawing3D.prototype.redraw = function() {
Expand All @@ -13632,12 +13632,12 @@ module.exports = function setupParser(Processing, options) {
p.specular(0, 0, 0);
p.emissive(0, 0, 0);
p.camera();
p.draw();

pmouseXLastFrame = p.mouseX;
pmouseYLastFrame = p.mouseY;
p.pmouseX = pmouseXLastEvent;
p.pmouseY = pmouseYLastEvent;
return p.draw().then(function() {
pmouseXLastFrame = p.mouseX;
pmouseYLastFrame = p.mouseY;
p.pmouseX = pmouseXLastEvent;
p.pmouseY = pmouseYLastEvent;
});
};

/**
Expand All @@ -13662,7 +13662,6 @@ module.exports = function setupParser(Processing, options) {
p.noLoop = function() {
doLoop = false;
loopStarted = false;
clearInterval(looping);
curSketch.onPause();
};

Expand All @@ -13682,17 +13681,21 @@ module.exports = function setupParser(Processing, options) {
timeSinceLastFPS = Date.now();
framesSinceLastFPS = 0;

looping = window.setInterval(function() {
try {
curSketch.onFrameStart();
p.redraw();
doLoop = true;
function looping() {
curSketch.onFrameStart();
p.redraw().then(function() {
curSketch.onFrameEnd();
} catch(e_loop) {
window.clearInterval(looping);
if (doLoop) {
setTimeout(function() { requestAnimationFrame(looping) }, curMsPerFrame);
}
}).catch(function(e_loop) {
throw e_loop;
}
}, curMsPerFrame);
doLoop = true;
});
}

looping();

loopStarted = true;
curSketch.onLoop();
};
Expand Down Expand Up @@ -13729,7 +13732,7 @@ module.exports = function setupParser(Processing, options) {
*/
p.exit = function() {
// cleanup
window.clearInterval(looping);
doLoop = false;
removeInstance(p.externals.canvas.id);
delete(curElement.onmousedown);

Expand Down Expand Up @@ -21567,24 +21570,29 @@ module.exports = function setupParser(Processing, options) {
curSketch.onLoad(processing);

// Run void setup()
if (processing.setup) {
processing.setup();
if (!processing.setup) {
processing.setup = function() { return Promise.resolve() };
}

if (!processing.draw) {
processing.draw = function() { return Promise.resolve() };
}

processing.setup().then(function() {
// if any transforms were performed in setup reset to identity matrix
// so draw loop is unpolluted
processing.resetMatrix();
curSketch.onSetup();
}

// some pixels can be cached, flushing
resetContext();
// some pixels can be cached, flushing
resetContext();

if (processing.draw) {
if (!doLoop) {
processing.redraw();
} else {
processing.loop();
}
}
});
} else {
window.setTimeout(function() { executeSketch(processing); }, retryInterval);
}
Expand Down
Loading

0 comments on commit 56085c6

Please sign in to comment.