mirror of
https://github.com/wassname/CanvasTextWrapper.git
synced 2026-06-27 22:20:17 +08:00
6 lines
4.3 KiB
JavaScript
6 lines
4.3 KiB
JavaScript
/*! CanvasTextWrapper
|
||
* https://github.com/namniak/CanvasTextWrapper
|
||
* Version: 0.4.0
|
||
* MIT License (http://www.opensource.org/licenses/mit-license.html)
|
||
*/
|
||
!function(a){function b(a,b,c){"use strict";function d(){s=c.font.match(/\d+(px|em|%)/g)?+c.font.match(/\d+(px|em|%)/g)[0].match(/\d+/g):18,t=0,u=[],v=[],w={x:0,y:0},f(s),g(),o(),e()}function e(){if(c.sizeToFill){var a=b.trim().split(/\s+/).length,d=0;do f(++d),x=d,h();while(B>t&&u.join(" ").split(/\s+/).length==a);f(--d),x=d}else h();c.justifyLines&&"auto"===c.lineBreak&&k(),n(),l()}function f(a){var b=c.sizeToFill?r.font.split(/\b\d+px\b/i):c.font.split(/\b\d+px\b/i);r.font=b[0]+a+"px"+b[1]}function g(){isNaN(c.lineHeight)?-1!==c.lineHeight.toString().indexOf("px")?x=parseInt(c.lineHeight):-1!==c.lineHeight.toString().indexOf("%")&&(x=parseInt(c.lineHeight)/100*s):x=s*c.lineHeight}function h(){if(c.allowNewLine)for(var a=b.trim().split("\n"),d=0,e=0;d<a.length-1;d++)e+=a[d].trim().split(/\s+/).length,v.push(e);var f=b.trim().split(/\s+/);i(f),j(f),t=u.length*x}function i(a){for(var b,c,d,e,f=0;f<a.length;f++)if(b="",c=r.measureText(a[f]).width,c>A){for(var g=0;r.measureText(b+a[f][g]).width<=A&&g<a[f].length;g++)b+=a[f][g];d=a[f].slice(0,g),e=a[f].slice(g),a.splice(f,1,d,e)}}function j(a){for(var b=0,d=0;b<a.length;d++)if(u[d]="","auto"===c.lineBreak){for(;r.measureText(u[d]+a[b]).width<=A&&b<a.length;)if(u[d]+=a[b]+" ",b++,c.allowNewLine)for(var e=0;e<v.length;e++)if(v[e]===b){d++,u[d]="";break}u[d]=u[d].trim()}else u[d]=a[b],b++}function k(){for(var a,b,c,d=0;d<u.length;d++)c=r.measureText(u[d]).width,(!a||c>a)&&(a=c,b=d);var e,f,g,h,i,j=" ";for(d=0;d<u.length;d++)if(d!==b&&(e=u[d].trim().split(/\s+/).length,!(1>=e))){u[d]=u[d].trim().split(/\s+/).join(j),f=r.measureText(j).width,g=(a-r.measureText(u[d]).width)/f,h=g/(e-1),i="";for(var k=0;h>k;k++)i+=j;u[d]=u[d].trim().split(j).join(i)}}function l(){for(var a=0;a<u.length;a++)m(u[a]),w.y=parseInt(w.y)+x,r.fillText(u[a],w.x,w.y),c.strokeText&&r.strokeText(u[a],w.x,w.y)}function m(a){"center"==c.textAlign?w.x=(y-r.measureText(a).width)/2:"right"==c.textAlign?w.x=y-r.measureText(a).width-c.paddingX:w.x=c.paddingX}function n(){"middle"==c.verticalAlign?w.y=(z-t)/2:"bottom"==c.verticalAlign?w.y=z-t-c.paddingY:w.y=c.paddingY}function o(){if(!(a instanceof HTMLCanvasElement))throw new TypeError("The first parameter must be an instance of HTMLCanvasElement.");if("string"!=typeof b)throw new TypeError("The second parameter must be a string.");if(isNaN(s))throw new TypeError('Cannot parse "font".');if(isNaN(x))throw new TypeError('Cannot parse "lineHeight".');if("left"!==c.textAlign.toLocaleLowerCase()&&"center"!==c.textAlign.toLocaleLowerCase()&&"right"!==c.textAlign.toLocaleLowerCase())throw new TypeError('Property "textAlign" must be set to either "left", "center", or "right".');if("top"!==c.verticalAlign.toLocaleLowerCase()&&"middle"!==c.verticalAlign.toLocaleLowerCase()&&"bottom"!==c.verticalAlign.toLocaleLowerCase())throw new TypeError('Property "verticalAlign" must be set to either "top", "middle", or "bottom".');if("boolean"!=typeof c.justifyLines)throw new TypeError('Property "justifyLines" must be set to a Boolean.');if(isNaN(c.paddingX))throw new TypeError('Property "paddingX" must be set to a Number.');if(isNaN(c.paddingY))throw new TypeError('Property "paddingY" must be set to a Number.');if("boolean"!=typeof c.fitParent)throw new TypeError('Property "fitParent" must be set to a Boolean.');if("auto"!==c.lineBreak.toLocaleLowerCase()&&"word"!==c.lineBreak.toLocaleLowerCase())throw new TypeError('Property "lineBreak" must be set to either "auto" or "word".');if("boolean"!=typeof c.sizeToFill)throw new TypeError('Property "sizeToFill" must be set to a Boolean.');if("boolean"!=typeof c.strokeText)throw new TypeError('Property "strokeText" must be set to a Boolean.')}var p={font:"18px Arial, sans-serif",sizeToFill:!1,lineHeight:1,allowNewLine:!0,lineBreak:"auto",textAlign:"left",verticalAlign:"top",justifyLines:!1,paddingX:0,paddingY:0,fitParent:!1,strokeText:!1};c=c||{};for(var q in p)p.hasOwnProperty(q)&&(c[q]=c&&c[q]?c[q]:p[q]);var r=a.getContext("2d");r.font=c.font,r.textBaseline="bottom";var s,t,u,v,w,x,y=c.fitParent===!1?a.width:a.parentNode.clientWidth,z=c.fitParent===!1?a.height:a.parentNode.clientHeight,A=y-2*c.paddingX,B=z-2*c.paddingY;d()}"module"in a&&"exports"in module?module.exports=b:a.CanvasTextWrapper=b}(this); |