简体   繁体   English

Fabric.js在画布上的图像上分层文本

[英]Fabric.js layering text over Images on a Canvas

So, I've just recently begun working with HTML5 and canvases , I'm currently working with fabric.js . 所以,我最近刚刚开始一起工作HTML5canvases ,我目前正在工作fabric.js But, I ran into an issue, and after spending the last 12 hours trying different things, I've decided to come here for help, so here it is: 但是,我遇到了一个问题,在花了最后12个小时尝试各种不同的事情之后,我决定来这里寻求帮助,所以这里是:

Using fabric.js, 使用fabric.js,

I've layered a bunch of images to make it how I want, but now I want to be able to write over those images using fabric.js. 我已经对一堆图像进行了分层,使其符合我的要求,但是现在我希望能够使用fabric.js覆盖这些图像。 I can only get it to write either all of them in the write spot, but wrong z-position(the words end up all BELOW the image and I think the background-image too - the words flash quickly then disappear), by putting it in the callback, all six text objects end in the exact same spot. 我只能把它全部写在写位置上,但是z位置错误(单词最终都在图像下方,我也认为背景图像-单词快速闪烁然后消失)在回调中,所有六个文本对象都在完全相同的位置结束。 So, how do I go about this? 那么,我该怎么做呢? I've tried bringToFront(), sendToBack(), moveTo() , and there was a function where it inserted it right into the spot, but I forgot it. 我尝试了bringToFront(), sendToBack(), moveTo() ,并且有一个函数将其插入sendToBack(), moveTo() ,但是我忘记了。 But, there's a good chance I ended up using them wrong because I'm very new to fabric.js. 但是,很有可能我最终错误地使用了它们,因为我对fabric.js还是很陌生。

Here's a link to the basic code: http://pastebin.com/TxNCKAkD 这是基本代码的链接: http : //pastebin.com/TxNCKAkD

But, the main places I guess I'd assume the issues are at are: 但是,我猜我认为问题出在哪里:

text[i] = new fabric.Text("ChiefKeef", {
       top: (5+(65*topShift)),
       left: leftShift+15,
      hasControls: false,
       fill: "orange",
      fontFamily: 'pokeText'
        });
canvas.add(text[i]);

The one above is in a for loop, and each add is put in an array. 上面的一个在for循环中,每个添加都放在一个数组中。
And here, which is a simpler place: 在这里,这是一个更简单的地方:

var tbText = new fabric.Text("Sentence", {
    top: 210,
    left: 15,
    hasControls: false,
    fill: "#32CD32",
    fontFamily: 'pokeText'
});
 canvas.add(tbText);

And both of these work, but the text is placed behind all of the images no matter what I've done. 这两种工作都可以,但是无论我做什么,文本都放置在所有图像的后面。 So, how do I get this text above the images I have placed already? 那么,如何在已放置的图像上方获取此文本? Thanks for reading and hopefully someone can help me out. 感谢您的阅读,希望有人可以帮助我。 If I missed anything, or you need extra info, I'll gladly provide it :) 如果我错过了任何事情,或者您需要其他信息,我们将很乐意提供:)

i added canvas.sendToBack(y); 我添加了canvas.sendToBack(y); & canvas.sendToBack(textBox); canvas.sendToBack(textBox); into the function body , where you add the panel png & the textbox png, and it works. 到函数主体中,在其中添加面板png和文本框png,即可使用。 The i-text objects ' ChiefKeef ' are above the pane image. i文本对象“ ChiefKeef ”位于窗格图像上方。

i 've made a jsfiddle with sample images because i could not reproduce yours, but you can see the result(by pressing the monkey :) ). 我用示例图像制作了一个jsfiddle,因为我无法复制您的图像,但是您可以看到结果(按猴子:))。 http://jsfiddle.net/5KKQ2/495/ http://jsfiddle.net/5KKQ2/495/

snippets: 片段:

x[i] = new
fabric.Image.fromURL("https://t2.ftcdn.net/jpg/00/97/16/03/500_F_97160389_tVczY4dIrtaRFkSMhV9elnU0NBMVJ1Y3.jpg", function(y){
    //just add canvas.sendToBack(y);
    canvas.add(y);canvas.sendToBack(y);
}, {
    top: (5+(65*topShift)),
    left: leftShift,
        height:65,
        width:195,
    borderColor: "blue",
    hasControls: true,
    lockMovementX: true,
    lockMovementY: true
});
var textBox = new fabric.Image.fromURL("https://t2.ftcdn.net/jpg/00/90/00/97/500_F_90009771_mWttXqopbyZZMVcyuXd5y2iQeeDK9NJm.jpg", function(textBox){
    //just add canvas.sendToBack(textBox);
    canvas.add(textBox);canvas.sendToBack(textBox);
}, {
    top: 200,
    left: 5,
        height:50,
        width:300,
    borderColor: "blue",
    hasControls: false,
    lockMovementX: true,
    lockMovementY: true
});

hope helps, good luck. 希望有帮助,祝你好运。

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

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