简体   繁体   English

使用ctx.drawImage()将画布另存为图像时,我无法将画布另存为图像

[英]I can't save the canvas as an image when I use ctx.drawImage() to put another image in it

Im trying to make drawing app where you can draw something on canvas and save your result as an image on the server by clicking "save" button. 我试图使绘图应用程序,您可以在画布上绘制一些东西,然后通过单击“保存”按钮将结果另存为服务器上的图像。 You can also put another image as background for your drawing. 您还可以将其他图像作为图形的背景。 The problem is that when I put an image to the canvas using ctx.drawImage() I can't save the canvas as an image because nothing happens. 问题是,当我使用ctx.drawImage()将图像放到画布上时,由于什么也没有发生,所以我无法将画布另存为图像。 Everything works ok until I use ctx.drawImage(). 一切正常,直到我使用ctx.drawImage()为止。 Why I can't save canvas as an image with another image in it? 为什么我不能将画布另存为图像?

My ajax code: 我的ajax代码:

// it works until I use ctx.drawImage()
$.ajax({
           type: "POST",
           url: "save.php",
           data: {image: dataURL},
           success: function()
            {
                alert('saved');
            }
        });

Code for putting another image as background: 用于将另一个图像作为背景的代码:

//var ctx = can.getContext('2d');
var img = new Image;
ctx.drawImage (img, 0, 0);

My PHP code: 我的PHP代码:

<?php
$dataURL = $_POST["image"];  
$parts = explode(',', $dataURL);  
$data = $parts[1];  
$data = base64_decode($data);  
$fp = fopen('test.png', 'w');  
fwrite($fp, $data);  
fclose($fp); 
?>

This is the entire javascript code 这是整个javascript代码

    $(document).ready (function()
    {

    var color = $("#color").val();


    $("#size").val("10");
    var mouse = 0;

    var can = document.getElementById("canny");
    var ctx = can.getContext('2d');

    var offsetX = 158;
    var offsetY = 200;

    var img = new Image;
    var url = "http://i.imgur.com/fmAoxZ0.jpg";
    img.src = url;



    function setBackground()
    {

        ctx.drawImage (img, 0, 0);
    }   






    function setOpacity(newValue)
    {
        $("#canny").css ("opacity", newValue * 0.01);
        $("#txt-opacity").html(newValue + "%");
    }


    $("body").mousedown(function(event)
    {

            color = $("#color").val();
            var cordX = event.clientX - offsetX;
            var cordY = event.clientY - offsetY;
            var size = $("#size").val();


            ctx.beginPath();
            ctx.arc(cordX,cordY,size,0,2*Math.PI);
            ctx.fillStyle = color;
            ctx.fill();


        document.getElementById("coords").innerHTML = "x: " + cordX + "     y: " + cordY;

        mouse = 1;

        $("body").mousemove(function(event)
        {
            if (mouse == 1)
            {

                var cordX = event.clientX - offsetX;
                var cordY = event.clientY - offsetY;
                var size = $("#size").val();


                ctx.beginPath();
                ctx.arc(cordX,cordY,size,0,2*Math.PI);
                ctx.fillStyle = color;
                ctx.fill();

                $("body").mouseup(function()
                {

                    mouse = 0;

                });


            }
        });

    mouse = 1;

    });


    $("#opacity").change (function()
    {
        setOpacity(this.value);
    });


    $("#opacity").trigger("change");





    $("#red").click (function()
    {
        $("#color").val("#FF3636");
        $(this).css ("border-color", "darkorange");
        $("#blue").css ("border-color", "black");
        $("#lime").css ("border-color", "black");
        $("#yellow").css ("border-color", "black");
    });


    $("#blue").click (function()
    {
        $("#color").val("#0080FF");
        $(this).css ("border-color", "darkorange");
        $("#red").css ("border-color", "black");
        $("#lime").css ("border-color", "black");
        $("#yellow").css ("border-color", "black");
    });


    $("#lime").click (function()
    {
        $("#color").val("#8CFF00");
        $(this).css ("border-color", "darkorange");
        $("#red").css ("border-color", "black");
        $("#blue").css ("border-color", "black");
        $("#yellow").css ("border-color", "black");
    });


    $("#yellow").click (function()
    {
        $("#color").val("#FFF01F");
        $(this).css ("border-color", "darkorange");
        $("#red").css ("border-color", "black");
        $("#blue").css ("border-color", "black");
        $("#lime").css ("border-color", "black");
    });





    $("#btn-clear").click(function()
    {


        if (confirm ("Are you sure to clear your image?"))
        {
            ctx.clearRect(0, 0, can.width, can.height);
        }

    });



    $("#btn-save").click (function()
    {
        var dataURL = can.toDataURL();


            $.ajax({
               type: "POST",
               url: "save.php",
               data: {image: dataURL},
               success: function()
                {
                    alert('saved');
                }
            });









    });


    $("#fill").click (function()
    {
        $("#canny").css ("background-color", color);

    });


    $('input[type=radio][name=bgselect]').change (function ()
    {
        if (this.value == "image")
        {
            setBackground();

            $("#url").css ("visibility", "visible");
            img.src = url;
        } else
        {
            $('#canny').css('background-image', 'none');
            $("#url").css ("visibility", "hidden");

        }
    });

    $("#url").change(function()
    {
        url = $(this).val();
        setBackground();

    });









});

Your problem is be with canvas.toDataURL which is disabled if you draw cross-domain images to your canvas. 您的问题出在canvas.toDataURL上,如果您将跨域图像绘制到画布上,则会被禁用。 If you open your browser console using F12 you will see the security error. 如果使用F12打开浏览器控制台,则会看到安全错误。

Since you're image host (imgur.com) has enabled cross-domain access to your image, you can comply with cross-domain security by adding img.crossOrigin='anonymous' to your img object. 由于您是图像托管者(imgur.com)已启用对图像的跨域访问,因此可以通过在img对象中添加img.crossOrigin='anonymous'来遵守跨域安全性。

var img = new Image;
img.crossOrigin='anonymous';
var url = "http://i.imgur.com/fmAoxZ0.jpg";
img.src = url;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM