[英]Add background image & draggable images to HTML5 canvas
This is my canvas image text editor and I want to add a background image and an image that can drag and drop. 这是我的画布图像文本编辑器,我想添加背景图像和可以拖放的图像。 I found code snippets to do things like that but with my code it is somewhat not working.
我发现代码片段可以执行类似的操作,但是对于我的代码来说,它却无法正常工作。
Code here 在这里编码
<style>canvas{width:450px; height:350px;}</style>
<canvas width="792" height="612"></canvas>
<a id="canvas-download" download="canvas-image.png" href="">Download</a>
<input type="file" id="bgload" name="bgload" />
<input type="file" id="logoload" name="logoload" />
<input id="title" value="[Certificate Title]"/>
<input id="present" value="[Presented to]" />
<input id="receiver" value="[Type Name Here]" />
<input id="awardDate" value="[Awarding Date]" />
<input id="signature" value="[Signature]" />
<script>
$(function () {
var cvs = $("canvas"),
cvsWidth = 792,
cvsHeight = 612,
ctx = cvs[0].getContext("2d"),
title = $("#title"),
present = $("#present"),
receiver = $("#receiver"),
awardDate = $("#awardDate"),
signature = $("#signature");
function writeCaption(text, y, size, x) {
var size = size;
do {
size--;
ctx.font = size + 'px Georgia';
ctx.lineWidth = size / 32;
} while (ctx.measureText(text).width > cvsWidth)
if (x == 0)
ctx.fillText(text, cvsWidth / 2, y);
else
ctx.fillText(text, x, y);
//ctx.strokeText(text, cvsWidth / 2, y);
}
// Setup basic canvas settings
$.extend(ctx, {
//strokeStyle: "#000000",
textAlign: 'center',
fillStyle: "#000",
lineCap: "round"
})
$("<img />")
.load(function () {
var img = this;
$(document.body).on("keyup", function () {
var titleText = title.val(),
presentText = present.val(),
signatureText = signature.val(),
awardDateText = awardDate.val(),
receiverText = receiver.val();
ctx.clearRect(0, 0, cvsWidth, cvsHeight);
ctx.drawImage(img, 0, 0, cvsWidth, cvsHeight);
//text alignments
ctx.textBaseline = 'top';
writeCaption(titleText, 150, 40, 0);
ctx.textBaseline = 'top';
writeCaption(presentText, 240, 20, 0);
ctx.textBaseline = 'top';
writeCaption(receiverText, 270, 30, 0);
ctx.textBaseline = 'bottom';
writeCaption(awardDateText, 490, 20, 300);
ctx.textBaseline = 'bottom';
writeCaption(signatureText, 490, 20, cvsWidth - 300 + ctx.measureText(signatureText).width);
}).trigger("keyup");
})
.attr("src", "<?php echo _SITE_URL; ?>images/editor/Template1.jpg")
.attr("id", "canvasimg");
var download = document.getElementById('canvas-download');
download.addEventListener('click', function () {
var canvas = document.getElementById('cs_canvas');
var data = canvas.toDataURL();
download.href = data;
}, false);
});
</script>
Here's the jsfiddle link for the working example. 这是工作示例的jsfiddle链接。 http://jsfiddle.net/57gfqsm3/7/
http://jsfiddle.net/57gfqsm3/7/
Please help 请帮忙
I spent a long time trying to drag and drop images on a canvas tag this and eventually used kinetic.js. 我花了很长时间尝试将图像拖放到画布标签上,并最终使用了dynamic.js。
https://github.com/ericdrowell/KineticJS/ https://github.com/ericdrowell/KineticJS/
I'll watch this post in case anyone has a better solution. 如果有人有更好的解决方案,我会看这篇文章。
I found a sophisticated javascript canvas library called Fabric. 我找到了一个称为Fabric的复杂javascript画布库。 Click Here
点击这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.