【发布时间】:2014-04-04 01:36:03
【问题描述】:
我最近发现使用 stage.toDataURL 将舞台内容保存为图像。(KineticJS 版本 5.0.1)
带有核心 HTML5 画布对象的自定义 Shape drawFunc 内容在保存图像后不会应用到图像中。
###################### 图片请查看附图以获得更好的清晰度。
1)原始画布(舞台)。
https://f.cloud.github.com/assets/1320926/2300781/c8167c30-a107-11e3-9b61-acf94005c252.png
2) 保存阶段内容后的图像 测试1
https://f.cloud.github.com/assets/1320926/2300784/fc5343b6-a107-11e3-8c69-07cccc4d7ac4.png
我们看到垂直字符串“Sample Text”不会被保存或者它是不可见的。
###################### 代码1)这是我的形状对象代码,其中线条垂直打印“示例文本”
var sampleText = new Kinetic.Shape({
drawFunc: function(context) {
var ctx=this.getContext()._context;
ctx.beginPath();
var maxWidth = 2;
var lineHeight = 25;
var x = 415;
var y = 700;
var text = 'Sample Text';
ctx.font = '15pt Calibri';
ctx.fillStyle = 'green';
//Function for vetical text display
wrapText(ctx, text, x, y, maxWidth, lineHeight);
ctx.closePath();
ctx.fill();
ctx.restore();
},
});
//如果需要,可以使用wrapText函数
function wrapText(context, text, x, y, maxWidth, lineHeight) {
var words = text.split('');
var line = '';
//alert(context);
for(var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = 20;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
}
else {
line = testLine;
}
}
context.fillText(line, x, y);
}
2)保存图片的阶段代码
stage.toDataURL({
callback: function(dataUrl) {
/*
* here you can do anything you like with the data url.
* In this tutorial we'll just open the url with the browser
* so that you can see the result as an image
*/
$('#theimage').attr('src',dataUrl);
$.ajax({
type: "POST",
url: "save.php",
data: { imageData: dataUrl },
});
}
});
请指教。
提前感谢您的时间和考虑。
-奈蒂克。
【问题讨论】: