Canvas.toDataURL() Uncaught TypeError: undefined is not a function(Canvas.toDataURL() Uncaught TypeError: undefined is not a function)
问题描述
我正在使用一个名为 html2canvas 的插件将我页面上的一些 html 转换为画布元素.然后我想将该画布保存为图像.不幸的是,我一直遇到标题中的错误.我尝试过使用不同的变量名、不同的 html 等.但一直遇到相同的错误.这是我的代码(点击按钮触发):
I'm using a plugin called html2canvas to convert some html on my page into a canvas element. I then want to save that canvas as an image. Unfortunately I keep encountering the error in the title. I have tried with different variable names, with different html, etc. But keep encountering the same error. Here is my code (triggered on a button click):
function generate(){
html2canvas($('#b2_1'), {
onrendered: function(canvas) {
canvas.setAttribute("id", "canvas");
document.body.appendChild(canvas);
}
});//this all works, the canvas appears as expected
var myCanvas = $(document).find('#canvas');
myCanvas.css("margin-left", "50px");//this was to test I was selecting the right element, the canvas moves
var myImg = myCanvas.toDataURL();//code breaks here
}
推荐答案
好的,我发现我的问题是我试图在我的 jQuery 对象而不是我的 canvas 元素上调用 toDataURL()
.为了解决这个问题,我使用了 .get(0)
.完整代码如下:
Ok, I found my problem was I was trying to call toDataURL()
on my jQuery object rather than my canvas element. To fix this I used .get(0)
. Full code below:
function generate(){
html2canvas($('#b2_1'), {
onrendered: function(canvas) {
canvas.setAttribute("id", "canvas");
document.body.appendChild(canvas);
}
});//this all works, the canvas appears as expected
var myCanvas = $(document).find('#canvas');
myCanvas.css("margin-left", "50px");
var myImg = myCanvas.get(0).toDataURL();//have to get the canvas element from the jquery object
}
这篇关于Canvas.toDataURL() Uncaught TypeError: undefined is not a function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Canvas.toDataURL() Uncaught TypeError: undefined is not a function
基础教程推荐
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 直接将值设置为滑块 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01