gpt4 book ai didi

javascript - 在新窗口中打开时,图像未在 Canvas 中绘制

转载 作者:行者123 更新时间:2023-12-03 07:12:58 25 4
gpt4 key购买 nike

我可以毫无问题地在 Canvas 上绘制图像。但我希望将图像绘制在新窗口中的 Canvas 上。变量具有正确的值,但仍然没有绘制任何内容。

这是我的代码:

var popup = window.open();

popup.oldCanvas= oldCanvas;

popup.url = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';

popup.document.open();

popup.document.write("<html><head>");

popup.document.write('<script src="lib/jquery/jquery-2.1.1.min.js"></script>');

popup.document.write("<script>\n");

popup.document.write("$(document).ready(function() { \n");

popup.document.write(" function createCanvas() { \n");

popup.document.write(" var img = new Image;\n");

popup.document.write(" var newCanvas = document.createElement('canvas');\n");

popup.document.write(" var context = newCanvas.getContext('2d'); \n");

popup.document.write(" newCanvas.width = oldCanvas.width; \n");

popup.document.write(" newCanvas.height = oldCanvas.height; \n");

popup.document.write(" img.onload = function() {\n");

popup.document.write(" context.drawImage(this, 0, 0); \n");

popup.document.write(" }; \n");

popup.document.write(" img.src = url; \n");

popup.document.write(" }; \n");

popup.document.write(" createCanvas(); \n");

popup.document.write("}); \n");

popup.document.write("</script>");

popup.document.write("</head><body>");

popup.document.write("</body>");

popup.document.write("</html>");

popup.document.close();

请告诉我我做错了什么,因为相同的代码在不在新窗口中绘制时工作正常。

最佳答案

Canvas 已创建,但从未插入到 DOM 中:

var newCanvas = document.createElement('canvas');
document.body.appendChild(newCanvas); // add to DOM
...

关于javascript - 在新窗口中打开时,图像未在 Canvas 中绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36551463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com