Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have searched a lot to drag the text drawn over it using fillText() method of JS over canvas.

Any help on this will be much appreciated...
Posted
Updated 1-May-21 0:57am

lets see if this one can help you

HTML
<html>
<head>

<script>
function allowDrop(ev)
{
	ev.preventDefault();
}

function drop(ev)
{
ev.preventDefault();
	var textToDraw=ev.dataTransfer.getData("Text");
	var ctx=document.getElementById("canv1").getContext("2d");	
	ctx.font = 'italic 15pt Calibri';
	ctx.fillText(textToDraw,10,50);
}
</script>
</head>
<body>

<p>Drag the W3Schools image into the rectangle:</p>

<canvas id="canv1" style="border:1px solid black" ondrop="drop(event)"  öndragover="allowDrop(event)"></canvas>
</body>
</html>
 
Share this answer
 
v2
canvas related variables
var canvas = document.getElementById("canvas-wrapper");
var ctx = canvas.getContext("2d");

// variables used to get mouse position on the canvas
var $canvas = $("#canvas-wrapper");
var canvasOffset = $canvas.offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var scrollX = $canvas.scrollLeft();
var scrollY = $canvas.scrollTop();

// variables to save last mouse position
// used to see how far the user dragged the mouse
// and then move the text by that distance
var startX;
var startY;

// an array to hold text objects
var texts = [];

// this var will hold the index of the hit-selected text
var selectedText = -1;

// clear the canvas & redraw all texts
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < texts.length; i++) {
var text = texts[i];
ctx.fillText(text.text, text.x, text.y);
}
}

// test if x,y is inside the bounding box of texts[textIndex]
function textHittest(x, y, textIndex) {
var text = texts[textIndex];
return (x >= text.x && x <= text.x + text.width && y >= text.y - text.height && y <= text.y);
}

// handle mousedown events
// iterate through texts[] and see if the user
// mousedown'ed on one of them
// If yes, set the selectedText to the index of that text
function handleMouseDown(e) {
e.preventDefault();
startX = parseInt(e.clientX - offsetX);
startY = parseInt(e.clientY - offsetY);
// Put your mousedown stuff here
for (var i = 0; i < texts.length; i++) {
if (textHittest(startX, startY, i)) {
selectedText = i;
}
}
}

// done dragging
function handleMouseUp(e) {
e.preventDefault();
selectedText = -1;
}

// also done dragging
function handleMouseOut(e) {
e.preventDefault();
selectedText = -1;
}

// handle mousemove events
// calc how far the mouse has been dragged since
// the last mousemove event and move the selected text
// by that distance
function handleMouseMove(e) {
if (selectedText < 0) {
return;
}
e.preventDefault();
mouseX = parseInt(e.clientX - offsetX);
mouseY = parseInt(e.clientY - offsetY);

// Put your mousemove stuff here
var dx = mouseX - startX;
var dy = mouseY - startY;
startX = mouseX;
startY = mouseY;

var text = texts[selectedText];
text.x += dx;
text.y += dy;
draw();
}

// listen for mouse events
$("#canvas-wrapper").mousedown(function (e) {
handleMouseDown(e);
});
$("#canvas-wrappers").mousemove(function (e) {
handleMouseMove(e);
});
$("#canvas-wrapper").mouseup(function (e) {
handleMouseUp(e);
});
$("#canvas-wrapper").mouseout(function (e) {
handleMouseOut(e);
});

$("#submit").click(function () {

// calc the y coordinate for this text on the canvas
var y = texts.length * 20 + 20;

// get the text from the input element
var text = {
text: $("#theText").val(),
x: 20,
y: y
};

// calc the size of this text for hit-testing purposes
ctx.font = "16px verdana";
text.width = ctx.measureText(text.text).width;
text.height = 16;

// put this new text in the texts array
texts.push(text);

// redraw everything
draw();

});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900