简体   繁体   English

使用Canvas fillText()-HTML5,JavaScript

[英]Using Canvas fillText() - HTML5, JavaScript

I have a plugin that will fill text based on an image using the following: 我有一个插件,可以使用以下内容填充基于图像的文本:

function draw() {
        //grab font to use
        var fFamily = $(".activeText a .fontType-cont").val();
        ctx.font = startFontSize + 'px ' + fFamily;
        ctx.fillText(text, 10, 100);

        ctx.globalCompositeOperation = 'source-in';
        ctx.drawImage(img, 10, 20, 500, 100);
}

var canvas = document.getElementById(current),
ctx = canvas.getContext('2d'),
img = document.createElement('img');
//draw it
img.onload = draw;

img.src = $(".activeGColor").find('img').attr('src');

What happens is that the canvas will take that image, display it in the canvas and as the text gets bigger, you see it as the fill for the text. 发生的事情是,画布将获取该图像,并将其显示在画布中,并且当文本变大时,您会将其视为文本的填充。 My issue is that, How can I make it so it does not look stretched or whatnot? 我的问题是,如何使它看起来不会拉伸或什么呢? Do I have an option to do so? 我可以选择这样做吗?

What are your ideas? 你有什么想法?

We need to see an example of whats going on here. 我们需要看一个例子。 Your question makes it sound like you have some sort of animation going on... that changes the font-size to a larger and larger size. 您的问题听起来好像您正在进行某种动画...将字体大小更改为越来越大的大小。 The way things work by default, is that the text will render proportionally. 默认情况下,工作方式是文本按比例呈现。 However if your using any thing like ctx.scale() you obviously could throw that off. 但是,如果您使用诸如ctx.scale()之类的东西,显然可以将其甩开。 Also if your not clearing your canvas each frame, you could get more of a bleed effect, that you may be describing as stretching. 另外,如果您不清除每个帧的画布,则可能会产生更多的出血效果,您可能将其描述为拉伸。

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

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