1

I have to modify this program to replace image with text. In this program, the value of input box should reflect in the place of balloon image. Could you help me ?

Program link:

$('#draw').click(function() {
    var text = document.getElementById('txtBallon').value;

        var $ballon = $('#ballon'),
            $canvas = $('#canvas');
        var ballon_x = $ballon.offset().left - $canvas.offset().left,
            ballon_y = $ballon.offset().top - $canvas.offset().top;

        context.drawImage(ballon, ballon_x, ballon_y);

        $ballon.hide();
        $(this).attr('disabled', 'disabled');
    });

JSFiddle

1
  • What actually want. You want to replace text written textinput in place of balloon when draw click? Commented Jan 6, 2016 at 6:09

2 Answers 2

1

You can do like following way:

context.font = "30px Verdana";
context.fillText(text,ballon_x,ballon_y);

Instead of context.drawImage(ballon, ballon_x, ballon_y);

Working Fiddle

Updated Fiddle with text drag

Sign up to request clarification or add additional context in comments.

16 Comments

thanks a lot. please could you make that text draggable ?
draggable after draw?
no before draw, to do that text should reflect simultaneously when i type in input box, then drag it and draw.
@VenkadeshS Check this. jsfiddle.net/V92Gn/3884. type in text. beside that label you can drag anywhere and click on draw. You can make changes as per your requirement.
yeah. draw is not working. but this is for your reference to complete it: jsfiddle.net/vengreat/7r2007nn/2
|
1

This is how I would do it:

$(document).ready(function() {
  var d_canvas = document.getElementById('canvas');
  var context = d_canvas.getContext('2d');
  var background = document.getElementById('background');
  context.drawImage(background, 0, 0);

  $('.drag').draggable();

  $('#draw').click(function() {
      var $canvas = $('#canvas');

var textX = $('#myText').offset().left - $canvas.offset().left;
var textY = $('#myText').offset().top - $canvas.offset().top + 12;
        context.font= "13px Arial";
        context.fillText($("#myText").text(),textX,textY);

        $('#myText').hide();
    $(this).attr('disabled', 'disabled');
  });
});

$("#theText").keyup(function(){
    $("#myText").text($(this).val());
});

Here is the updated JSFiddle demo

14 Comments

Hi., you are merely come to answer. But My expectation is "text should reflect simultaneously when i type in input box, then drag it and draw finally".
@VenkadeshS You mean you should be able to type the text, then drag it, then click draw to fix it on the canvas? Or did I misunderstand?
I updated my answer. The checkbox is at the bottom :)
The initial position is defined by the CSS class .drag :)
Here it is......just moved the JS to the bottom as I suggested before
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.