html5 canvas save,javascript - how to save html5 canvas to server - Stack Overflow

Im loading a few images to my canvas and then after they load I want to click a button that saves that canvas image to my server. I can see the script works fine until it gets to the 'toDataURL' part and my function stops executing. What am I doing wrong? Here is my code:

body {

margin: 0px;

padding: 0px;

}

height="200">

Save

function loadImages(sources, callback)

{

var images = {};

var loadedImages = 0;

var numImages = 0;

// get num of sources

for(var src in sources) {

numImages++;

}

for(var src in sources) {

images[src] = new Image();

images[src].onload = function() {

if(++loadedImages >= numImages)

{

callback(images);

}

};

images[src].src = sources[src];

}

}

var canvas =

document.getElementById('myCanvas');

var context = canvas.getContext('2d');

var sources = {

great:

'images/great.jpg',

star:

'images/1Star.jpg', good:

'images/good.jpg'

};

loadImages(sources, function(images) {

context.drawImage(images.great,

0, 0, 80, 120);

context.drawImage(images.star, 80,

0, 80, 120);

context.drawImage(images.good, 160, 0, 80,

120);

});

function saveCards()

{

var canvas=

document.getElementById("myCanvas");

alert("stops");

var theString= canvas.toDataURL();

var postData= "CanvasData="+theString;

var ajax= new XMLHttpRequest();

ajax.open("POST", 'saveCards.php', true);

ajax.setRequestHeader('Content-Type',

'canvas/upload');

ajax.onreadystatechange=function()

{

if(ajax.readyState == 4)

{

alert("image was saved");

}else{

alert("image was not saved");

}

}

ajax.send(postData);

}

Thank you for any help is it because the images are not loaded before toDataUrl is called? If so can you please help me fix it.

This is the php script:

if(isset($GLOBALS['HTTP_RAW_POST_DATA']));

{

$rawImage=$GLOBALS['HTTP_RAW_POST_DATA'];

$removeHeaders=

substr($rawImage,strpos($rawImage, ",")+1);

$decode=base64_decode($removeHeaders);

$fopen= fopen('images/image.png', 'wb');

fwrite($fopen, $decode);

fclose($fopen);

}

?>

I am getting a security error though.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML5 canvas 是一个强大的绘图API,提供了一些方法来帮助开发者控制绘图状态。其中两个非常有用的方法是 save() 和 restore()。 save() 方法用于保存当前绘图状态,包括当前的变换矩阵、剪切区域和样式属性等。调用 save() 方法后,我们可以对绘图状态进行任意修改,而不会影响到之前保存的状态。 restore() 方法用于恢复之前保存的绘图状态。调用 restore() 方法后,我们可以回到之前保存的状态,并且继续在该状态下进行绘图操作。 这两个方法通常是成对使用的。例如,我们可以在进行一些复杂的绘图操作之前调用 save() 方法来保存当前状态,然后在绘制完成后调用 restore() 方法来恢复之前的状态。这样可以避免对后续绘图操作造成影响。 下面是一个简单的示例代码,演示了如何在 canvas 中使用 save() 和 restore() 方法: ```html <canvas id="myCanvas" width="200" height="100"></canvas> <script> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); // 保存当前状态 ctx.save(); // 绘制一个矩形,并修改一些属性 ctx.fillStyle = "red"; ctx.fillRect(10, 10, 50, 50); ctx.strokeStyle = "blue"; ctx.lineWidth = 3; ctx.strokeRect(20, 20, 30, 30); // 恢复之前的状态 ctx.restore(); // 绘制一个圆形,此时样式属性已经恢复到之前的状态 ctx.beginPath(); ctx.arc(100, 50, 30, 0, 2*Math.PI); ctx.fillStyle = "green"; ctx.fill(); </script> ``` 在上面的代码中,我们首先调用了 save() 方法来保存当前状态,然后绘制了一个红色填充、蓝色边框的矩形。接着,我们又调用了 restore() 方法来恢复之前保存的状态。最后,我们绘制了一个绿色的圆形,此时样式属性已经恢复到之前的状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值