Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

javascript - How to insert input text into image?

I want to develop extension for magento which help to create custom t-shirt but i don't know how to do it.I want to provide functionality like this site http://www.inkpixi.com/item/ATV+Repair/A252/329/item.htmlDead link

here you enter the name and then name automatically insert to image .i want to provide this exactly but didn't get right matter

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do it so simply with

var canvas = document.createElement('canvas'),
  ctx = canvas.getContext('2d');

canvas.width  = 200;
canvas.height = 200;
document.body.appendChild(canvas);

function sendToCanvas( ob ){
  var img = new Image();
  img.addEventListener('load', function(){
    ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
    ctx.font = ob.fontWeight+' '+ob.fontSize+' '+ob.fontFamily;
    ctx.textAlign = 'center';
    ctx.fillStyle = ob.color;
    ctx.fillText(ob.text, canvas.width/2, canvas.height/2.5);
  });
  img.src = ob.image;
}

// Defaults
var cvsObj = {
    text       : "stackoverflow",
    image      : "http://i.imgur.com/hqayV16.jpg",
    fontFamily : "Arial",
    fontWeight : "bold",
    fontSize   : "90%",
    color      : "rgba(244, 128, 36, 0.7)"
};

sendToCanvas( cvsObj ); // Send default data on init

document.getElementById("text").addEventListener("input", function(){
  cvsObj.text = this.value; // Modify cvsObj text
  sendToCanvas( cvsObj );   // Send custom data on input
});

 
Text: <input id="text" type="text"><br>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...