项目需要将canvas元素转换为图像。我有一些问题,并试图创建一个简单的项目,我可以做一些实验。此项目应该将canvas元素转换为图像并将其附加到div但它似乎不起作用。请参阅下面的代码我该怎么办?
谢谢, 马特
/**
* Ken Fyrstenberg Nilsen
* Abidas Software
*/
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
/**
* Demonstrates how to download a canvas an image with a single
* direct click on a link.
*/
function doCanvas() {
/* draw something */
ctx.fillStyle = '#f90';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#fff';
ctx.font = '60px sans-serif';
ctx.fillText('Code Project IE', 10, canvas.height / 2 - 15);
ctx.font = '26px sans-serif';
ctx.fillText('Click link above to save this as image', 15, canvas.height / 2 + 35);
}
function downloadCanvas(link, canvasId, filename) {
var canvas = document.getElementById(canvasId);
var imgData = canvas.toDataURL();
var img = new Image();
img.onload = split_4;
img.src = imgData;
img.appendTo($('#imagediv'));
}
function split_4() {
alert('did something happen?')
};
/**
* The event handler for the link's onclick event. We give THIS as a
* parameter (=the link element), ID of the canvas and a filename.
*/
document.getElementById('download').addEventListener('click', function() {
downloadCanvas(this, 'canvas', 'test.png');
}, false);
/**
* Draw something to canvas
*/
doCanvas();
body {
background-color:#555557;
padding:0;
margin:0;
overflow:hidden;
font-family:sans-serif;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
canvas {
border:1px solid #000;
float:left;
clear:both;
}
#download {
float:left;
cursor:pointer;
color:#ccc;
padding:3px;
}
#download:hover {
color:#fff;
}
/*
div, input {
font-size:16px;
font-family:sans-serif;
border:1px solid #000;
border-radius: 5px;
float:left;
padding:5px;
width:50px;
margin:1px 1px;
background-color:#bbb;
}
input[type='text'] {
font-size:16px;
font-weight:bold;
width:70px;
text-align:center;
background-color:#fff;
padding-bottom:4px;
}
input[type='button'] {
font-size:16px;
font-weight:bold;
width:110px;
text-align:center;
background-color:#333;
color:#eee;
padding-bottom:4px;
}
input[type='button']:hover {
background-color:#fff463;
color:#000;
}
input[type='range'] {
width:100px;
margin:0 0 0 10px;
}
*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="download">Click to Create and Append Image from Canvas Element</a>
<canvas width="500" height="300" id="canvas">Sorry, no canvas available</canvas>
<div id="imagediv"></div>
答案 0 :(得分:1)
而不是img.appendTo($('#imagediv'));
,请$('#imagediv').append(img);
。干杯!
/**
* Ken Fyrstenberg Nilsen
* Abidas Software
*/
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
/**
* Demonstrates how to download a canvas an image with a single
* direct click on a link.
*/
function doCanvas() {
/* draw something */
ctx.fillStyle = '#f90';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#fff';
ctx.font = '60px sans-serif';
ctx.fillText('Code Project IE', 10, canvas.height / 2 - 15);
ctx.font = '26px sans-serif';
ctx.fillText('Click link above to save this as image', 15, canvas.height / 2 + 35);
}
function downloadCanvas(link, canvasId, filename) {
var canvas = document.getElementById(canvasId);
var imgData = canvas.toDataURL();
var img = new Image();
img.onload = split_4;
img.src = imgData;
$('#imagediv').append(img);
}
function split_4() {
alert('did something happen?')
};
/**
* The event handler for the link's onclick event. We give THIS as a
* parameter (=the link element), ID of the canvas and a filename.
*/
document.getElementById('download').addEventListener('click', function() {
downloadCanvas(this, 'canvas', 'test.png');
}, false);
/**
* Draw something to canvas
*/
doCanvas();
body {
background-color:#555557;
padding:0;
margin:0;
overflow:hidden;
font-family:sans-serif;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
canvas {
border:1px solid #000;
float:left;
clear:both;
}
#download {
float:left;
cursor:pointer;
color:#ccc;
padding:3px;
}
#download:hover {
color:#fff;
}
/*
div, input {
font-size:16px;
font-family:sans-serif;
border:1px solid #000;
border-radius: 5px;
float:left;
padding:5px;
width:50px;
margin:1px 1px;
background-color:#bbb;
}
input[type='text'] {
font-size:16px;
font-weight:bold;
width:70px;
text-align:center;
background-color:#fff;
padding-bottom:4px;
}
input[type='button'] {
font-size:16px;
font-weight:bold;
width:110px;
text-align:center;
background-color:#333;
color:#eee;
padding-bottom:4px;
}
input[type='button']:hover {
background-color:#fff463;
color:#000;
}
input[type='range'] {
width:100px;
margin:0 0 0 10px;
}
*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="download">Click to Create and Append Image from Canvas Element</a>
<canvas width="500" height="300" id="canvas">Sorry, no canvas available</canvas>
<div id="imagediv"></div>