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

Support HTML elements as textures when DOM/canvas are available #87

Merged
merged 1 commit into from
Dec 7, 2017
Merged
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
44 changes: 40 additions & 4 deletions webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3482,6 +3482,36 @@ function checkFormat (format) {
format === gl.RGBA)
}

var extractImageData = function (pixels) {
if (typeof pixels === 'object' && typeof pixels.width !== 'undefined' && typeof pixels.height !== 'undefined') {
if (typeof pixels.data !== 'undefined') {
return pixels
}

var context = null

if (typeof pixels.getContext === 'function') {
context = pixels.getContext('2d')
} else if (typeof pixels.src !== 'undefined' && typeof document === 'object' && typeof document.createElement === 'function') {
var canvas = document.createElement('canvas')

if (typeof canvas === 'object' && typeof canvas.getContext === 'function') {
context = canvas.getContext('2d')

if (context !== null) {
context.drawImage(pixels, 0, 0)
}
}
}

if (context !== null) {
return context.getImageData(0, 0, pixels.width, pixels.height)
}
}

return null
}

var _texImage2D = gl.texImage2D
gl.texImage2D = function texImage2D (
target,
Expand All @@ -3498,9 +3528,12 @@ gl.texImage2D = function texImage2D (
type = height
format = width

if (typeof pixels !== 'object' || typeof pixels.data !== 'object') {
throw new TypeError('texImage2D(GLenum, GLint, GLenum, GLint, GLenum, GLenum, ImageData)')
pixels = extractImageData(pixels)

if (pixels == null) {
throw new TypeError('texImage2D(GLenum, GLint, GLenum, GLint, GLenum, GLenum, ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement)')
}

width = pixels.width
height = pixels.height
pixels = pixels.data
Expand Down Expand Up @@ -3613,9 +3646,12 @@ gl.texSubImage2D = function texSubImage2D (
type = height
format = width

if (typeof pixels !== 'object' || typeof pixels.data !== 'object') {
throw new TypeError('texSubImage2D(GLenum, GLint, GLint, GLint, GLenum, GLenum, ImageData)')
pixels = extractImageData(pixels)

if (pixels == null) {
throw new TypeError('texSubImage2D(GLenum, GLint, GLint, GLint, GLenum, GLenum, ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement)')
}

width = pixels.width
height = pixels.height
pixels = pixels.data
Expand Down