0
JsFiddle:http://jsfiddle.net/K34a7/我正在創建一個div並在其中保存圖像。我想知道如何檢測在圖像中寫入的部分的寬度,以便我可以動態分配div的寬度大小而不是整個圖像?捕獲寫在畫布上的圖像的寬度
document.getElementById('text').addEventListener('keyup', function(){
tCtx.canvas.width = tCtx.measureText(this.value).width;
var t = this.value.split('\n');
tCtx.canvas.height = t.length*10;
console.log(t);
var y = 10;
for(var i=0; i<t.length; i++, y+=10) {
tCtx.fillText(t[i], 0, y);
}
//console.log('Value: ' + this.value);
imageElem.src = tCtx.canvas.toDataURL();
//console.log(imageElem.src);
}, false);
可能重複:http://stackoverflow.com/questions/10099226/determine-width-of-string-in-html5-canvas – devnull69