简体   繁体   English

无法定位Canvas的子元素

[英]Unable to target child element of Canvas

Here is my code 这是我的代码

    javascript: (function() {
    var canvas = document.getElementById("app-view").children[1];
    var img = canvas.toDataURL("image/png");
    window.open(img);
    })();

and here is the app element where I wish to target the second canvas element "children 1 " but it's not working for some reason and always gets stuck at the first element. 这是app元素 ,我希望将第二个canvas元素“ children 1 ”作为目标,但由于某种原因它无法正常工作,并且总是卡在第一个元素上。 A solution will be much appreciated, thanks in advance! 一个解决方案将不胜感激,在此先感谢! 在此处输入图片说明

In the picture it always targets the first canvas in yellow but I need the one displayed in blue. 在图片中,它始终以黄色为第一个画布,但我需要以蓝色显示。 You can test the code by saving the script as a bookmark in your browser. 您可以通过将脚本另存为浏览器中的书签来测试代码。

Haven't worked out why toDataURL gets the data of the left canvas, but if you wanted to save the canvas, you can isolate it with 还没有弄清楚为什么toDataURL获取左画布的数据,但是如果要保存画布,可以使用以下方法隔离它

var can = document.getElementById("app-view").children[1];
jQuery("body > *").remove();
jQuery("body").append(can) ;

and then now you can right click and save the canvas 然后现在您可以右键单击并保存画布

这对我有用:

document.getElementById('app-view').childNodes[1]

无法使用<canvas>里面的元素<div>元素</div><div id="text_translate"><p>我无法在 &lt;div&gt; 元素中添加 &lt;canvas&gt; animation 。 使用当前代码,animation 占据整页,我想将 &lt;canvas&gt; 封装在 &lt;div&gt; 中,以便 animation 进入单个部分。 类似于 &lt;div&gt;&lt;canvas&gt; &lt;/canvas&gt;&lt;/div&gt;</p><pre> HTML: &lt;canvas&gt;&lt;/canvas&gt; &lt;h2&gt;hi&lt;/h2&gt;</pre><pre> CSS: * { padding: 0; margin: 0; box-sizing: border-box; } body { width: 100%; height: 100vh; font-family: 'Poppins', sans-serif; background-color: #121212; display: flex; align-items: center; justify-content: center; } canvas { width: 100%; height: 100%; position: fixed; top: 0; left: 0; z-index: 0; } h2 { font-size: 80px; color: white; position: relative; }</pre></div></canvas> - Unable to use <canvas> element inside <div> element

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 目标对象 <li> 元件 - Target child of <li> element 无法获取目标元素 - Unable to get the target element 无法使用$(this)定位元素 - Unable to target element using $(this) 有没有办法获得画布的子元素? - Is there a way to get child element of canvas? 子元素上的JavaScript目标数据 - JavaScript target data on child element 悬停元素的目标子元素(animejs) - Target child element of hovered element (animejs) 画布元素无法绘制图像 - canvas element unable to draw image JavaScript 画布将元素移动到目标坐标 - JavaScript canvas moving element to target coordinates 无法使用<canvas>里面的元素<div>元素</div><div id="text_translate"><p>我无法在 &lt;div&gt; 元素中添加 &lt;canvas&gt; animation 。 使用当前代码,animation 占据整页,我想将 &lt;canvas&gt; 封装在 &lt;div&gt; 中,以便 animation 进入单个部分。 类似于 &lt;div&gt;&lt;canvas&gt; &lt;/canvas&gt;&lt;/div&gt;</p><pre> HTML: &lt;canvas&gt;&lt;/canvas&gt; &lt;h2&gt;hi&lt;/h2&gt;</pre><pre> CSS: * { padding: 0; margin: 0; box-sizing: border-box; } body { width: 100%; height: 100vh; font-family: 'Poppins', sans-serif; background-color: #121212; display: flex; align-items: center; justify-content: center; } canvas { width: 100%; height: 100%; position: fixed; top: 0; left: 0; z-index: 0; } h2 { font-size: 80px; color: white; position: relative; }</pre></div></canvas> - Unable to use <canvas> element inside <div> element 向目标内的子元素添加样式 - Add style to child element inside target
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM