From 092afcc4cf4683d2496141ac5564a42afdb8d9d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Gottero?= Date: Thu, 26 Feb 2015 11:31:39 +0100 Subject: [PATCH] fix #26 Patch proposal to fix #26 When DOM elements are detached from the document, offsetWidth/Height are always 0: whereas the element may have proper size set in width/height attributes. This patch adds a fallback on width/height properties when offsetWidth/Height are 0. It allows to use a canvas as a texture without having to attach it first. --- .../core/client/renderers/WebGLRenderer.java | 12 +++++++-- .../core/client/textures/Texture.java | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/thothbot/parallax/core/client/renderers/WebGLRenderer.java b/src/thothbot/parallax/core/client/renderers/WebGLRenderer.java index cab3167e..30b17798 100644 --- a/src/thothbot/parallax/core/client/renderers/WebGLRenderer.java +++ b/src/thothbot/parallax/core/client/renderers/WebGLRenderer.java @@ -2981,8 +2981,8 @@ public void setTexture( Texture texture, int slot ) getGL().pixelStorei( PixelStoreParameter.UNPACK_ALIGNMENT, texture.getUnpackAlignment() ); Element image = texture.getImage(); - boolean isImagePowerOfTwo = Mathematics.isPowerOfTwo( image.getOffsetWidth() ) - && Mathematics.isPowerOfTwo( image.getOffsetHeight() ); + boolean isImagePowerOfTwo = Mathematics.isPowerOfTwo( texture.getWidth() ) + && Mathematics.isPowerOfTwo( texture.getHeight() ); texture.setTextureParameters( getGL(), getMaxAnisotropy(), TextureTarget.TEXTURE_2D, isImagePowerOfTwo ); @@ -3029,6 +3029,10 @@ private CanvasElement createPowerOfTwoImage(Element image) { int width = image.getOffsetWidth(); int height = image.getOffsetHeight(); + if (width == 0 && height == 0) { + width = image.getPropertyInt("width"); + height = image.getPropertyInt("height"); + } CanvasElement canvas = Document.get().createElement("canvas").cast(); @@ -3055,6 +3059,10 @@ private Element clampToMaxSize ( Element image, int maxSize ) { int imgWidth = image.getOffsetWidth(); int imgHeight = image.getOffsetHeight(); + if (imgWidth == 0 && imgHeight == 0) { + imgWidth = image.getPropertyInt("width"); + imgHeight = image.getPropertyInt("height"); + } if ( imgWidth <= maxSize && imgHeight <= maxSize ) return image; diff --git a/src/thothbot/parallax/core/client/textures/Texture.java b/src/thothbot/parallax/core/client/textures/Texture.java index 2b8a5c65..6b71033d 100644 --- a/src/thothbot/parallax/core/client/textures/Texture.java +++ b/src/thothbot/parallax/core/client/textures/Texture.java @@ -328,6 +328,32 @@ public void setImage(Element image) { this.image = image; } + /** + * Gets texture width. + * + * @return the texture width. + */ + public int getWidth() { + int width = image.getOffsetWidth(); + if (width == 0) { + width = image.getPropertyInt("width"); + } + return width; + } + + /** + * Gets texture height. + * + * @return the texture height. + */ + public int getHeight() { + int height = image.getOffsetHeight(); + if (height == 0) { + height = image.getPropertyInt("height"); + } + return height; + } + /** * Gets texture offset. *