最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Add a Textarea inside the canvas at the current mouse position - Stack Overflow

programmeradmin0浏览0评论

I want to add some textual information on the canvas. When i click mouse on a point of the canvas it should shown a text area at the current mouse position. It should be also possible to select,drag and rotate the textarea.How can achieve this functionality using HTML5 canvas and javascript?

I want to add some textual information on the canvas. When i click mouse on a point of the canvas it should shown a text area at the current mouse position. It should be also possible to select,drag and rotate the textarea.How can achieve this functionality using HTML5 canvas and javascript?

Share Improve this question asked Dec 11, 2012 at 7:22 KiranPalodeKiranPalode 4983 gold badges8 silver badges23 bronze badges 1
  • 1 Show some code please. You know the drill. – DrinkJavaCodeJava Commented Dec 11, 2012 at 7:23
Add a ment  | 

2 Answers 2

Reset to default 5

The code below is that provided by dreame4 adapted to allow dragging (jsfiddle).

var canvas = document.getElementById("c"),
    textarea = null;

function mouseDownOnTextarea(e) {
    var x = textarea.offsetLeft - e.clientX,
        y = textarea.offsetTop - e.clientY;
    function drag(e) {
        textarea.style.left = e.clientX + x + 'px';
        textarea.style.top = e.clientY + y + 'px';
    }
    function stopDrag() {
        document.removeEventListener('mousemove', drag);
        document.removeEventListener('mouseup', stopDrag);
    }
    document.addEventListener('mousemove', drag);
    document.addEventListener('mouseup', stopDrag);
}
canvas.addEventListener('click', function(e) {
    if (!textarea) {
        textarea = document.createElement('textarea');
        textarea.className = 'info';
        textarea.addEventListener('mousedown', mouseDownOnTextarea);
        document.body.appendChild(textarea);
    }
    var x = e.clientX - canvas.offsetLeft,
        y = e.clientY - canvas.offsetTop;
    textarea.value = "x: " + x + " y: " + y;
    textarea.style.top = e.clientY + 'px';
    textarea.style.left = e.clientX + 'px';
}, false);​

However, rotation requires quite a different and more plicated solution - make the text within the canvas using context.fillText and then see this post on how to rotate it. You'll need to explicitly keep track of the position and angle of rotation of the text area. The event listener for the canvas element will have to check whether the mouse is within the text, in which case it starts dragging, or outside, in which case it creates/moves the text.

Example of code that handles click on a canvas and displays textarea with coordinates:

HTML

<canvas id="c" width="200" height="200"></canvas>

JS:

var canvas = document.getElementById("c"),
    textarea = null;

canvas.addEventListener('click', function(e) {
    if(!textarea) {
        textarea = document.createElement('textarea');
        textarea.className = 'info';
        document.body.appendChild(textarea);
    }
    var x = e.clientX - canvas.offsetLeft,
    y = e.clientY - canvas.offsetTop;
    textarea.value = "x: " + x + " y: " + y;
    textarea.style.top = e.clientY + 'px';
    textarea.style.left = e.clientX + 'px';
}, false);

The rest of the functionality is more plex. Probably you'd like to use external library like jQuery UI.

EDITED: missing 'px' in styles. Thanks Stuart.

发布评论

评论列表(0)

  1. 暂无评论