Skip to content

Instantly share code, notes, and snippets.

@ingeit
Last active August 4, 2017 15:34
Show Gist options
  • Save ingeit/12c39ae2b25d804f018ce425b5c8fde7 to your computer and use it in GitHub Desktop.
Save ingeit/12c39ae2b25d804f018ce425b5c8fde7 to your computer and use it in GitHub Desktop.
Escribir en imagen con canvas
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
canvas.width = $('img').width();
canvas.crossOrigin = "Anonymous";
canvas.height = $('img').height();
ctx.drawImage($('img').get(0), 0, 0);
ctx.font = "36pt Verdana";
$(document).on('input','#inp',function(){
//redraw image
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.drawImage($('img').get(0), 0, 0);
//refill text
ctx.fillStyle = "red";
ctx.fillText($(this).val(),40,80);
ctx.fillStyle = "blue";
ctx.fillText('kevin',40,120);
});
$('button').click(function(){
console.log(ctx.getImageData(50, 50, 100, 100));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<img style="display:none" src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTsEbEB4kEMHt3WJ13HpZE16hJ3iKrE8ugnErvyln7oNPDma48U" crossorigin="anonymous"/>
<input type="text" id="inp"/>
<button type="submit">Save</button>
</form>
<canvas id="canvas" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment