html2canvas根本不起作用 [英] html2canvas not working at all
问题描述
这是不起作用的代码.基本上,我允许用户在div中编辑一些文本,然后显示其屏幕截图.有人可以告诉我为什么它不起作用吗?
Here is the code which isn't working. Basically I'm allowing the user to edit some text in the div and then display its screenshot. Can someone please tell why is it not working?
<!doctype html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="main.css" />
<title>Poster</title>
</head>
<body>
<div id="main_image">
<img src="image.jpg" id= "image"/>
<div id="user_text">IT IS TOTALLY AWESOME</div>
</div>
<div id="edit_text_box">
<textarea id="user_input" placeholder="Your text here.." rows="2" cols="30" ></textarea>
<div id="change_size">
<span style="float: left;">FONT-SIZE : </span>
<button id="decrease_size">-</button>
<button id="increase_size">+</button>
</div>
<input id="submit" type="submit" value="SUBMIT" />
</div>
<script type="text/javascript">
window.onload = function() {
html2canvas( [ document.body ], {
onrendered: function( canvas ) {
document.body.appendChild( canvas );
}
});
};
</script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/edit_text.js"></script>
</body>
</html>
推荐答案
我对您的代码做了一些改动,并且可以正常工作:
I changed your code a little bit and it works:
<!doctype html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="main.css" />
<title>Poster</title>
<script type="text/javascript" src="html2canvas.js"></script>
<script type="text/javascript">
var Submit = function() {
html2canvas(document.body, {
onrendered: function(canvas) {
document.body.appendChild(canvas);
}
});
};
</script>
</head>
<body>
<div id="main_image">
<img src="image.jpg" id= "image"/>
<div id="user_text">IT IS TOTALLY AWESOME</div>
</div>
<div id="edit_text_box">
<textarea id="user_input" placeholder="Your text here.." rows="2" cols="30" ></textarea>
<div id="change_size">
<span style="float: left;">FONT-SIZE : </span>
<button id="decrease_size">-</button>
<button id="increase_size">+</button>
</div>
<button onclick="Submit();" >Submit</button>
</div>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/edit_text.js"></script>
</body>
</html>
首先,我添加了html2canvas JavaScript文件(<script type="text/javascript" src="html2canvas.js"></script>
).您可以在 https://github.com/niklasvh/下载html2canvas/releases/download/0.4.1/html2canvas.js 并将该文件放到与HTML相同的文件夹中.显然,您正在尝试创建类似表单的内容,但是您的代码中没有form
标记.您可以在提交表单时(必须先添加<form>
)或使用button
的onclick
属性来调用html2canvas.我将submit
输入元素更改为button
,并将Submit
方法绑定到其onclick
属性.当用户单击按钮时,屏幕截图将显示在表单下方.
First, I added html2canvas JavaScript file (<script type="text/javascript" src="html2canvas.js"></script>
). You can download it at https://github.com/niklasvh/html2canvas/releases/download/0.4.1/html2canvas.js and put that file to the same folder as your HTML. Apparently, you are trying to create something like a form, but there's no form
tag in your code. You can call html2canvas either when you submit a form (you have to add <form>
then) or using onclick
attribute of a button
. I changed your submit
input element to button
and bind Submit
method to its onclick
attribute. When user clicks the button, screenshot appears below the form.
这篇关于html2canvas根本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!