简体   繁体   English

在画布元素中绘制文本

[英]Drawing text in canvas element

could you tell me what is the problem with the text on my canvas? 您能告诉我画布上的文字有什么问题吗? I tried to change the text size if I make it bigger it looks fine but if I make it smaller it looks weird. 我尝试更改文本大小,如果我放大它看起来很好,但是如果我缩小它看起来很奇怪。 I'm not sure what exactly affect the text; 我不确定到底是什么会影响文字; I tried to play with font property but with no result, whenever I make the font smaller I got the result like in the snippet below here is my code 我尝试使用font属性,但是没有结果,每当我缩小字体时,我都会得到如下代码所示的结果,这是我的代码

 (function(){ function init(){ var canvas = document.getElementsByTagName('canvas')[0]; var c = canvas.getContext('2d'); var animationStartTime =0; c.fillStyle = 'white'; c.font = 'normal 10pt'; var textarray = 'you can live a beatiful life with a postive mind'; var j=1; //time - next repaint time - HRT function draw(time){ c.fillText(textarray.substr(0,j),10,70); if(time - animationStartTime > 100){ animationStartTime = time; j++; } if( j <= textarray.length){ requestAnimationFrame(draw); // 17ms } } function start(){ animationStartTime = window.performance.now(); requestAnimationFrame(draw); } start(); } //invoke function init once document is fully loaded window.addEventListener('load',init,false); }()); //self invoking function 
 #canvas{ overflow:hidden; background-image: url('http://linux2.gps.stthomas.edu/~algh3635/practice/proj/img/img4.jpeg'); background-repeat: no-repeat; background-size: cover; } canvas { width:100%; height:80vh; left: 0; margin: 0; padding: 0; position: relative; top: 0; } 
 <div id="canvas"> <div class="row"> <div class="col-lg-12 text-center" > <canvas > </canvas> </div> </div> </div> 

When you fillText on the canvas, it stops being letters and starts being a letter-shaped collection of pixels. 当您在画布上填充文本时,它不再是字母,而开始是字母形状的像素集合。 When you zoom in on it, the pixels become bigger. 放大时,像素变大。 That's how a canvas works. 这就是画布的工作方式。

When you want the text to scale as a vector-based font and not as pixels, don't draw them on the canvas. 当您希望文本缩放为基于矢量的字体而不是像素时,请不要在画布上绘制它们。 You could create HTML elements instead and place them on top of the canvas using CSS positioning. 您可以改为创建HTML元素,然后使用CSS定位将其放在画布上。 That way the rendering engine will render the fonts in a higher resolution when you zoom in and they will stay sharp. 这样,渲染引擎将在放大时以更高的分辨率渲染字体,并且字体将保持清晰。 But anything you draw on the canvas will zoom accordingly. 但是您在画布上绘制的所有内容都会相应缩放。

But what I would recommend for this simple example is just doing it in html, css, and javascript like so: 但是,对于这个简单的示例,我建议在html,css和javascript中执行以下操作:

 var showText = function (target, message, index, interval) { if (index < message.length) { $(target).append(message[index++]); setTimeout(function () { showText(target, message, index, interval); }, interval); } } $(function () { showText("#text", "you can live a beatiful life with a postive mind", 0, 100); }); 
 #canvas{ position: relative; width: 100%; height: 80vh; overflow:hidden; background-image: url('http://linux2.gps.stthomas.edu/~algh3635/practice/proj/img/img4.jpeg'); background-repeat: no-repeat; background-size: cover; } #text { position: absolute; top: 50%; left: 20px; color: #fff; font-size: 20px; } 
 <div id="canvas"> <p id="text"> </p> </div> 

I'll leave it up to you to position the text and whatnot but you can see in this example that you can make the text as small, or as big, as you want. 我将由您自己决定放置文本的位置,但在此示例中您会看到,可以根据需要使文本变小或变大。

However 然而

...if you still absolutely need it to be a canvas then the following example works. ...如果您仍然绝对需要将其用作画布,则可以使用以下示例。 It works because the canvas is not being stretched by css, the width and height are predefined. 之所以起作用,是因为画布没有被CSS拉伸,宽度和高度是预定义的。

The canvas element runs independent from the device or monitor's pixel ratio. canvas元素独立于设备或监视器的像素比率运行。

On the iPad 3+, this ratio is 2. This essentially means that your 1000px width canvas would now need to fill 2000px to match it's stated width on the iPad display. 在iPad 3+上,该比例为2。这实际上意味着您现在需要将1000px宽度的画布填充2000px,以与其在iPad显示屏上声明的宽度匹配。 Fortunately for us, this is done automatically by the browser. 对我们来说幸运的是,这是由浏览器自动完成的。 On the other hand, this is also the reason why you see less definition on images and canvas elements that were made to directly fit their visible area. 另一方面,这也是为什么在为直接适合其可见区域而制作的图像和画布元素上看到较少定义的原因。 Because your canvas only knows how to fill 1000px but is asked to draw to 2000px, the browser must now intelligently fill in the blanks between pixels to display the element at its proper size. 因为您的画布只知道如何填充1000px,但被要求绘制为2000px,所以浏览器现在必须智能地填充像素之间的空白,以将元素显示为适当的大小。

I would highly recommend you read this article from HTML5Rocks which explains in more detail how to create high definition elements. 我强烈建议您阅读HTML5Rocks的这篇文章,其中更详细地说明了如何创建高清元素。

 var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); ctx.font = "12px Arial"; ctx.fillText("Hello World",10,50); 
 #canvas{ position: relative; overflow:hidden; background-image: url('http://linux2.gps.stthomas.edu/~algh3635/practice/proj/img/img4.jpeg'); background-repeat: no-repeat; background-size: cover; } 
 <!DOCTYPE html> <html> <body> <canvas id="canvas" width="800" height="500" style="border:1px solid #d3d3d3;"> Your browser does not support the canvas element. </canvas> </body> </html> 

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

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