简体   繁体   English

如何在HTML画布中创建动态绘图文本框

[英]How to create a dynamic drawing text box in html canvas

I don't have an idea on how to implement dynamic drawing box for text. 我对如何实现文本动态绘图框一无所知。

Basically here is the sample output: 基本上这是示例输出:

在此处输入图片说明

Note: 注意:

The box should be infront if there is another object in the canvas. 如果画布中还有另一个对象,则该框应位于最前面。

Update 更新

Here is my code so far: 到目前为止,这是我的代码:

MouseDown(){
//if first mousedown
     X1 = tempX; //tempX is for current positon
     Y1 = tempY;
     if (Input.style.display === "none") {
        Input.value = "";
        Input.style.top = tempX + 'px';
        Input.style.left = tempY + 'px';
        Input.size = 1;
        Input.style.display = "block";
        Draw();
        }
}

Draw(){
    var userInput = Input.value;
    rectangledText(X1, Y1, 150, userInput, 12, 'verdana'); //code by markE
}

 function rectangledText(x, y, width, text, fontsize, fontface) { //code by markE
        self.TextWidth = ctx.measureText(Text).width;
        var height = wrapText(x, y, text, fontsize, fontface, width)

        ctx.strokeRect(x, y, width, height);

    }


    function wrapText(x, y, text, fontsize, fontface, maxwidth) {
        var startingY = y;
        var words = text.split(' ');
        var line = '';
        var space = '';
        var lineHeight = fontsize * 1.286;
        ctx.font = fontsize + "px " + fontface;
        ctx.textAlign = 'left';
        ctx.textBaseline = 'top'
        for (var n = 0; n < words.length; n++) {
            var testLine = line + space + words[n];
            space = ' ';
            if (ctx.measureText(testLine).width > maxwidth) {
                ctx.fillText(line, x, y);
                line = words[n] + ' ';
                y += lineHeight;
                space = '';
            } else {
                line = testLine;
            }
        }
        ctx.fillText(line, x, y);
        return (y + lineHeight - startingY);
    }

And sample output with explanation : 并带有说明的示例输出 在此处输入图片说明

The issue is how I can make the first mousedown rectangle and text be wrapped using the Input.style 问题是如何使用Input.style使第一个mousedown矩形和文本换行

Html5 canvas does not have any native text input capability. HTML5 canvas没有任何本地文本输入功能。

IMO, don't try to make canvas do what it was not intended to do. 海事组织,不要试图让画布做它本来不想做的事情。

Instead, use CSS to position a input-type=text over the desired part of the canvas. 而是使用CSS在画布的所需部分上放置一个input-type=text The user can enter their text into this input. 用户可以将其文本输入到此输入中。

When the user has entered their desired text you can draw their text in a rectangle on the canvas like this: 用户输入所需的文本后,可以在画布上的矩形中绘制其文本,如下所示:

 var canvas=document.getElementById("canvas"); var ctx=canvas.getContext("2d"); var cw=canvas.width; var ch=canvas.height; var text='Lorem ipsum dolor sit amet, vel te vocent bonorum singulis. Quem magna commune nam in. Ut eos oportere persecuti efficiantur.'; rectangledText(50,50,150,text,12,'verdana'); function rectangledText(x,y,width,text,fontsize,fontface){ var height=wrapText(x,y,text,fontsize,fontface,width) ctx.strokeRect(x,y,width,height); } function wrapText(x,y,text,fontsize,fontface,maxwidth){ var startingY=y; var words = text.split(' '); var line = ''; var space=''; var lineHeight = fontsize*1.286; ctx.font = fontsize + "px " + fontface; ctx.textAlign='left'; ctx.textBaseline='top' for (var n=0; n<words.length; n++) { var testLine = line + space + words[n]; space=' '; if (ctx.measureText(testLine).width > maxwidth) { ctx.fillText(line,x,y); line = words[n] + ' '; y += lineHeight; space=''; } else { line = testLine; } } ctx.fillText(line, x,y); return(y+lineHeight-startingY); } 
 body{ background-color: ivory; } #canvas{border:1px solid red; margin:0 auto; } 
 <canvas id="canvas" width=300 height=300></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM