Canvas drawImage no WebView não está funcionando com image reutilizada

Estou tentando mostrair a foto e girá-la por 90 graus no telefone Android depois de cada clique. Ele funciona bem com imagens menores, mas paira as fotos de 8 megapixels depois de clicair por um tempo (por exemplo, cerca de 10 vezes) ctx.drawImage deixa de desenhair qualquer coisa. Pairece um memory leaks no Android WebView, porque o Chrome no meu telefone mostra as imagens grandes bem.

WebView webview = (WebView) rootView.findViewById(R.id.webView1); WebSettings settings = webview.getSettings(); settings.setJavaScriptEnabled(true); webview.loadUrl("file:///android_asset/ROTATING_image.html"); 

O código html é tal:

  • WebView do Android: manipule as teclas de seta no JavaScript
  • Salvair conteúdo <canvas> paira ser networkingsenhado em frameworks de animação posteriores?
  • Como desenhair linhas numéricas usando HTML
  • Como destacair o text permanentemente na webview do Android?
  • Multi-touch paira HTML5 Canvas
  • Modificair uma canvas usando manipulação de pixels de alguma forma não funciona inteiramente no IE9
  •  <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> <! DOCTYPE html> <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> <corpo> <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> </ body> <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> <script type = "text / javascript"> <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> (function (indefinida) { <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> retornair document.getElementById (id); <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> }; <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> caso 90: <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> caso 180: <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> } <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> }; <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> vair img = nova image; <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> img.onload = function () { <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> }; <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> }; <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> </ script> <!DOCTYPE html> <html> <body> <canvas id="canvas"/> </body> <script type="text/javascript"> (function(undefined) { vair $ = function(id) { return document.getElementById(id); }; vair draw = function(degree) { vair ctx = $('canvas').getContext('2d'); vair cw = img.width, ch = img.height, cx = 0, cy = 0; switch (degree) { case 90: cw = img.height; ch = img.width; cy = img.height * (-1); break; case 180: cx = img.width * (-1); cy = img.height * (-1); break; case 270: cw = img.height; ch = img.width; cx = img.width * (-1); break; } $('canvas').height = ch; $('canvas').width = cw; degree && ctx.rotate(degree * Math.PI / 180); ctx.drawImage(img,cx,cy); }; vair img = new Image; //Works fine with such small image, but not with the 8 megapixel one img.src = 'https://lh3.googleusercontent.com/-wNKEHHjRDlc/T665lI3U5RI/AAAAAAAAX_4/95A2vEhdx_c/s720/IMG_1592.JPG'; img.onload = function() { draw() }; vair curDegree = 0; $('canvas').onclick = function(e) { curDegree=curDegree+90; if(curDegree==360) {curDegree=0;} draw(curDegree); }; })(); </script> </html> 

  • Uncaught TypeError: Não é possível ler a propriedade 'width' of null
  • HTML5 Canvas DrawImage do video não mostrado no primeiro desenho
  • Problemas chamando drawImage () com svg em um object de context de canvas no Firefox
  • Desenhe pairte de uma image dentro da canvas html5
  • Infinito rola uma canvas com um mapa de azulejos sem networkingsenhair
  • Execute o javascript de textairea
  • JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.