admin管理员组文章数量:1318570
I am having a problem with a canvas element. It does not show when it is in a hidden div and the div is toggled. better explanation and example here
/
This is happening in ff and chrome (not tested others). Could anyone tell me why / how to overe this issue.
I am having a problem with a canvas element. It does not show when it is in a hidden div and the div is toggled. better explanation and example here
http://jsfiddle/78sJT/10/
This is happening in ff and chrome (not tested others). Could anyone tell me why / how to overe this issue.
Share Improve this question asked Jan 21, 2012 at 1:13 dogmatic69dogmatic69 7,5854 gold badges33 silver badges50 bronze badges2 Answers
Reset to default 2The issue is that dygraphs detects the div's width/height as 0/0 and doesn't learn otherwise until it's redrawn (there is no JS event which fires when an individual DOM element is resized).
The easiest workaround is to just explicitly let the dygraph know it should update its size and redraw itself after the div is toggled using g.resize();
$(document).ready(function(){
$('.click').click(function(){
$('#hidden').toggle();
g.resize();
});
});
I updated your jsfiddle http://jsfiddle/78sJT/13/
I just hit this issue in Chrome 37.0.2062.124 m, matching the problem described in the OP's question. However I'm not using any particular graphing library, just drawing to canvas. And I was already re-drawing the canvas when the enclosing DIV's size changed.
I find that if a canvas's width or height is set to zero, it bees unreliable. The next time you set the width and height to non-zero values, any drawing you do on it immediately will be ignored, even though its width and height are now non-zero and it can be seen in the element tree (and hover-highlighted in the page) in Chrome's debugger.
So in addition to the advice in benni_mac_b's answer you must either:
- use
setTimeout
or similar to delay drawing on the canvas, so you don't do it immediately after setting width/height, or - make sure you never set the canvas to zero width/height. Make sure it is at least 1 pixel.
I do the later for simplicity and it solves the problem.
In summary:
A canvas that has just grown from (0, 0) to (1000, 1000) cannot be drawn on immediately - it ignores all drawing instructions. But if it has just grown from (1, 1) to (1000, 1000) you can immediately draw on all of it.
本文标签: javascriptcanvas in a div with display none does not workStack Overflow
版权声明:本文标题:javascript - canvas in a div with display none does not work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742046937a2417842.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论