admin管理员组

文章数量:1401122

I am trying to delete Text in my canvas element, without loosing the Background-Image of the Canvas-Element.

I think I need to save the Imagesrc and give it back to the Canvas-Element after a clearRect, but I don't know how to do it.

I hope somebody can help me.

I am trying to delete Text in my canvas element, without loosing the Background-Image of the Canvas-Element.

I think I need to save the Imagesrc and give it back to the Canvas-Element after a clearRect, but I don't know how to do it.

I hope somebody can help me.

Share Improve this question edited Aug 25, 2013 at 22:34 Kev 120k53 gold badges306 silver badges393 bronze badges asked Aug 25, 2013 at 16:49 FeralusFeralus 1672 silver badges11 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 6

If you want a permanent image to write/erase/rewrite text on, you might try layering a canvas over an image:

HTML

    <div id="wrapper">
        <img id="bkImage" src="yourImage.png" width=300 height=300></canvas>
        <canvas id="canvas" width=300 height=300></canvas>
    </div>

CSS

    body{ background-color: ivory; padding:20px;}
    #wrapper{
        position:relative;
        width:300px;
        height:300px;
    }
    #bkImage{
        position:absolute; top:0px; left:0px;
        border:1px solid green;
        width:100%;
        height:100%;
    }
    #canvas{
        position:absolute; top:0px; left:0px;
        border:3px solid red;
        width:100%;
        height:100%;
    }

Then you can erase/rewrite text without having to clear the background image.

    function drawText(text,fill,stroke){
            ctx.clearRect(0,0,canvas.width,canvas.height);
            ctx.font="123px verdana";
            ctx.fillStyle=fill;
            ctx.strokeStyle=stroke;
            ctx.lineWidth=4;
            ctx.fillText(text,10,120);
            ctx.strokeText(text,10,120);
    }

Here’s code and a Fiddle: http://jsfiddle/m1erickson/fg5sV/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery./jquery.min.js"></script>

<style>
    body{ background-color: ivory; padding:20px;}
    #wrapper{
        position:relative;
        width:300px;
        height:300px;
    }
    #bkImage{
        position:absolute; top:0px; left:0px;
        border:1px solid green;
        width:100%;
        height:100%;
    }
    #canvas{
        position:absolute; top:0px; left:0px;
        border:3px solid red;
        width:100%;
        height:100%;
    }

</style>

<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");

    function drawText(text,fill,stroke){
            ctx.clearRect(0,0,canvas.width,canvas.height);
            ctx.font="123px verdana";
            ctx.fillStyle=fill;
            ctx.strokeStyle=stroke;
            ctx.lineWidth=4;
            ctx.fillText(text,10,120);
            ctx.strokeText(text,10,120);
    }

    drawText("Hi!","orange","green");

    $("#remove").click(function(){
        ctx.clearRect(0,0,canvas.width,canvas.height);
    });        
    $("#original").click(function(){
        drawText("Hi!","orange","green");
    });        
    $("#changed").click(function(){
        drawText("Bye!","purple","yellow");
    });        

}); // end $(function(){});
</script>

</head>

<body>
    <button id="remove">Remove the text</button>
    <button id="changed">Change the text</button>
    <button id="original">Original text</button>
    <div id="wrapper">
        <img id="bkImage" src="https://dl.dropboxusercontent./u/139992952/stackoverflow/KoolAidMan.png" width=300 height=300></canvas>
        <canvas id="canvas" width=300 height=300></canvas>
    </div>
</body>
</html>

Instead of using Text on the canvas, you can use HTML-Text.

本文标签: javascriptDelete only Text but not the Image using CanvasStack Overflow