-
Notifications
You must be signed in to change notification settings - Fork 75
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
Add video quads #745
Merged
Merged
Add video quads #745
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
910b149
Update quadFeature documentation.
manthey 10145ea
Improve testing infrastructure.
manthey d807e6b
Add video quads.
manthey 4bf0410
Update example and add tutorials.
manthey 74782d0
Add tests.
manthey 4b95cbd
Minor formatting changes and more comments.
manthey fa2e59e
Add a video transport tutorial.
manthey 66b6608
Fix a comment. Wrap some lines.
manthey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
var inherit = require('../inherit'); | ||
var registerFeature = require('../registry').registerFeature; | ||
var quadFeature = require('../quadFeature'); | ||
var util = require('../util'); | ||
|
||
/** | ||
* Create a new instance of class quadFeature. | ||
|
@@ -46,57 +47,158 @@ var canvas_quadFeature = function (arg) { | |
m_this.buildTime().modified(); | ||
}; | ||
|
||
/** | ||
* When any quad may have changed, ask for a animation frame callback so we | ||
* can update the quad on the next animation cycle. | ||
* | ||
* This is called when a video qaud may have changed play state. | ||
* @param {object} quad The quad record that triggered this. | ||
* @param {jQuery.Event} [evt] The event that triggered this. | ||
*/ | ||
this._checkQuadUpdate = function (quad, evt) { | ||
m_this.layer().map().scheduleAnimationFrame(m_this._checkIfChanged); | ||
}; | ||
|
||
/** | ||
* Check if any video quads are changing or need rerendering. If any are | ||
* changing (because they are seeking), defer rendering and check again. If | ||
* any need rendering, schedule it. | ||
*/ | ||
this._checkIfChanged = function () { | ||
if (!m_quads || !m_quads.vidQuads || !m_quads.vidQuads.length) { | ||
return; | ||
} | ||
var render = false, changing = false; | ||
|
||
$.each(m_quads.vidQuads, function (idx, quad) { | ||
if (quad.video && quad.video.HAVE_CURRENT_DATA !== undefined) { | ||
if (!quad.video.seeking && quad.video.readyState >= quad.video.HAVE_CURRENT_DATA) { | ||
render = true; | ||
} | ||
if (!quad.video.paused || quad.video.seeking) { | ||
changing = true; | ||
} | ||
} | ||
}); | ||
if (render) { | ||
m_this.renderer()._render(); | ||
} | ||
if (changing) { | ||
m_this.layer().map().scheduleAnimationFrame(m_this._checkIfChanged); | ||
} | ||
}; | ||
|
||
/** | ||
* Render all of the color quads. | ||
* | ||
* @param {CanvasRenderingContext2D} context2d The rendering context. | ||
* @param {geo.map} map The current renderer's parent map. | ||
*/ | ||
this._renderColorQuads = function (context2d, map) { | ||
// Not implemented yet. | ||
if (!m_quads.clrQuads || !m_quads.clrQuads.length) { | ||
return; | ||
} | ||
var oldAlpha = context2d.globalAlpha; | ||
var opacity = oldAlpha; | ||
$.each(m_quads.clrQuads, function (idx, quad) { | ||
var p0 = map.gcsToDisplay({x: quad.pos[0], y: quad.pos[1]}, null), | ||
p1 = map.gcsToDisplay({x: quad.pos[3], y: quad.pos[4]}, null), | ||
p2 = map.gcsToDisplay({x: quad.pos[6], y: quad.pos[7]}, null), | ||
p3 = map.gcsToDisplay({x: quad.pos[9], y: quad.pos[10]}, null); | ||
if (quad.opacity !== opacity) { | ||
opacity = quad.opacity; | ||
context2d.globalAlpha = opacity; | ||
} | ||
context2d.fillStyle = util.convertColorToHex(quad.color, true); | ||
context2d.beginPath(); | ||
context2d.moveTo(p0.x, p0.y); | ||
context2d.lineTo(p1.x, p1.y); | ||
context2d.lineTo(p3.x, p3.y); | ||
context2d.lineTo(p2.x, p2.y); | ||
context2d.closePath(); | ||
context2d.fill(); | ||
}); | ||
if (opacity !== oldAlpha) { | ||
context2d.globalAlpha = oldAlpha; | ||
} | ||
}; | ||
|
||
/** | ||
* Render all of the image quads. | ||
* Render all of the image and video quads. | ||
* | ||
* @param {CanvasRenderingContext2D} context2d The rendering context. | ||
* @param {geo.map} map The current renderer's parent map. | ||
*/ | ||
this._renderImageQuads = function (context2d, map) { | ||
if (!m_quads.imgQuads.length) { | ||
this._renderImageAndVideoQuads = function (context2d, map) { | ||
if ((!m_quads.imgQuads || !m_quads.imgQuads.length) && (!m_quads.vidQuads || !m_quads.vidQuads.length)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we wrap this long line? |
||
return; | ||
} | ||
|
||
var oldAlpha = context2d.globalAlpha; | ||
var opacity = oldAlpha; | ||
$.each(m_quads.imgQuads, function (idx, quad) { | ||
if (!quad.image) { | ||
$.each([m_quads.imgQuads, m_quads.vidQuads], function (listidx, quadlist) { | ||
if (!quadlist) { | ||
return; | ||
} | ||
var w = quad.image.width, | ||
h = quad.image.height; | ||
// Canvas transform is affine, so quad has to be a parallelogram | ||
// Also, canvas has no way to render z. | ||
var p0 = map.gcsToDisplay({x:quad.pos[0], y:quad.pos[1]}, null), | ||
p3 = map.gcsToDisplay({x:quad.pos[9], y:quad.pos[10]}, null), | ||
p2 = map.gcsToDisplay({x:quad.pos[6], y:quad.pos[7]}, null); | ||
context2d.setTransform((p3.x - p2.x) / w, (p3.y - p2.y) / h, | ||
(p0.x - p2.x) / w, (p0.y - p2.y) / h, | ||
p2.x, p2.y); | ||
if (quad.opacity !== opacity) { | ||
opacity = quad.opacity; | ||
context2d.globalAlpha = opacity; | ||
} | ||
if (!quad.crop) { | ||
context2d.drawImage(quad.image, 0, 0); | ||
} else { | ||
context2d.drawImage(quad.image, 0, 0, quad.crop.x, quad.crop.y, 0, 0, | ||
quad.crop.x, quad.crop.y); | ||
} | ||
$.each(quadlist, function (idx, quad) { | ||
var src, w, h; | ||
if (quad.image) { | ||
src = quad.image; | ||
w = src.width; | ||
h = src.height; | ||
} else if (quad.video) { | ||
src = quad.video; | ||
w = src.videoWidth; | ||
h = src.videoHeight; | ||
if (src.seeking) { | ||
return; | ||
} | ||
} | ||
if (!src || !w || !h || quad.opacity <= 0) { | ||
return; | ||
} | ||
// Canvas transform is affine, so quad has to be a parallelogram | ||
// Also, canvas has no way to render z. | ||
var p0 = map.gcsToDisplay({x: quad.pos[0], y: quad.pos[1]}, null), | ||
p2 = map.gcsToDisplay({x: quad.pos[6], y: quad.pos[7]}, null), | ||
p3 = map.gcsToDisplay({x: quad.pos[9], y: quad.pos[10]}, null); | ||
context2d.setTransform((p3.x - p2.x) / w, (p3.y - p2.y) / w, | ||
(p0.x - p2.x) / h, (p0.y - p2.y) / h, | ||
p2.x, p2.y); | ||
if (quad.opacity !== opacity) { | ||
opacity = quad.opacity; | ||
context2d.globalAlpha = opacity; | ||
} | ||
if (!quad.crop) { | ||
context2d.drawImage(src, 0, 0); | ||
} else { | ||
context2d.drawImage(src, 0, 0, quad.crop.x, quad.crop.y, 0, 0, | ||
quad.crop.x, quad.crop.y); | ||
} | ||
}); | ||
}); | ||
if (opacity !== oldAlpha) { | ||
context2d.globalAlpha = oldAlpha; | ||
} | ||
context2d.setTransform(1, 0, 0, 1, 0, 0); | ||
}; | ||
|
||
/** | ||
* If this returns true, the render will be skipped and tried again on the | ||
* next animation frame. | ||
* | ||
* @returns {boolean} Truthy to delay rendering. | ||
*/ | ||
this._delayRender = function () { | ||
var delay = false; | ||
if (m_quads && m_quads.vidQuads && m_quads.vidQuads.length) { | ||
$.each(m_quads.vidQuads, function (idx, quad) { | ||
if (quad.video && quad.video.HAVE_CURRENT_DATA !== undefined) { | ||
delay |= (quad.video.seeking && quad.delayRenderWhenSeeking); | ||
} | ||
}); | ||
} | ||
return delay; | ||
}; | ||
|
||
/** | ||
|
@@ -106,8 +208,10 @@ var canvas_quadFeature = function (arg) { | |
* @param {geo.map} map The current renderer's parent map. | ||
*/ | ||
this._renderOnCanvas = function (context, map) { | ||
this._renderImageQuads(context, map); | ||
this._renderColorQuads(context, map); | ||
if (m_quads) { | ||
this._renderImageAndVideoQuads(context, map); | ||
this._renderColorQuads(context, map); | ||
} | ||
}; | ||
|
||
/** | ||
|
@@ -121,6 +225,7 @@ var canvas_quadFeature = function (arg) { | |
} | ||
|
||
m_this.updateTime().modified(); | ||
m_this.layer().map().scheduleAnimationFrame(m_this._checkIfChanged); | ||
}; | ||
|
||
/** | ||
|
@@ -146,12 +251,13 @@ inherit(canvas_quadFeature, quadFeature); | |
|
||
// Now register it | ||
var capabilities = {}; | ||
capabilities[quadFeature.capabilities.color] = false; | ||
capabilities[quadFeature.capabilities.color] = true; | ||
capabilities[quadFeature.capabilities.image] = true; | ||
capabilities[quadFeature.capabilities.imageCrop] = true; | ||
capabilities[quadFeature.capabilities.imageFixedScale] = true; | ||
capabilities[quadFeature.capabilities.imageFull] = false; | ||
capabilities[quadFeature.capabilities.canvas] = true; | ||
capabilities[quadFeature.capabilities.video] = true; | ||
|
||
registerFeature('canvas', 'quad', canvas_quadFeature, capabilities); | ||
module.exports = canvas_quadFeature; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should it be if (!features[i]._delayRender())? If delayRender is true which means you want to skip rendering or something I missed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling
render()
reschedules the rendering to the next animation frame. Thereturn
then stops it from rendering right now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, thanks perhaps adding a comment here might help but I am fine either way.