dist/jcanvas.js
function _wrapText(ctx, params) {
	var allText = String(params.text),
		// Maximum line width (optional)
		maxWidth = params.maxWidth,
		// Lines created by manual line breaks (\n)
		manualLines = allText.split('\n'),
		// All lines created manually and by wrapping
		allLines = [],
		// Other variables
		lines, line, l,
		text, words, w;

	// Loop through manually-broken lines
	for (l = 0; l < manualLines.length; l += 1) {

		text = manualLines[l];
		// Split line into list of words
		words = text.split(' ');
		lines = [];
		line = '';

		// If text is short enough initially
		// Or, if the text consists of only one word
		if (words.length === 1 || ctx.measureText(text).width < maxWidth) {

			// No need to wrap text
			lines = [text];

		} else {

			// Wrap lines
			for (w = 0; w < words.length; w += 1) {

				// Once line gets too wide, push word to next line
				if (ctx.measureText(line + words[w]).width > maxWidth) {
					// This check prevents empty lines from being created
					if (line !== '') {
						lines.push(line);
					}
					// Start new line and repeat process
					line = '';
				}
				// Add words to line until the line is too wide
				line += words[w];
				// Do not add a space after the last word
				if (w !== (words.length - 1)) {
					line += ' ';
				}
			}
			// The last word should always be pushed
			lines.push(line);

		}
		// Remove extra space at the end of each line
		allLines = allLines.concat(
			lines
			.join('\n')
			.replace(/((\n))|($)/gi, '$2')
			.split('\n')
		);

	}

	return allLines;
}
function _wrapText(ctx, params) {
	var allText = String(params.text),
		// Maximum line width (optional)
		maxWidth = params.maxWidth,
		// Lines created by manual line breaks (\n)
		manualLines = allText.split('\n'),
		// All lines created manually and by wrapping
		allLines = [],
		// Other variables
		lines, line, l,
		text, words, w;

	// Loop through manually-broken lines
	for (l = 0; l < manualLines.length; l += 1) {

		text = manualLines[l];
		// Split line into list of words
		words = text.split(' ');
		lines = [];
		line = '';

		// If text is short enough initially
		// Or, if the text consists of only one word
		if (words.length === 1 || ctx.measureText(text).width < maxWidth) {

			// No need to wrap text
			lines = [text];

		} else {

			// Wrap lines
			for (w = 0; w < words.length; w += 1) {

				// Once line gets too wide, push word to next line
				if (ctx.measureText(line + words[w]).width > maxWidth) {
					// This check prevents empty lines from being created
					if (line !== '') {
						lines.push(line);
					}
					// Start new line and repeat process
					line = '';
				}
				// Add words to line until the line is too wide
				line += words[w];
				// Do not add a space after the last word
				if (w !== (words.length - 1)) {
					line += ' ';
				}
			}
			// The last word should always be pushed
			lines.push(line);

		}
		// Remove extra space at the end of each line
		allLines = allLines.concat(
			lines
			.join('\n')
			.replace(/((\n))|($)/gi, '$2')
			.split('\n')
		);

	}

	return allLines;
}
function _wrapText ( ctx , params ) { var allText = String ( params . text ) , // Maximum line width (optional) maxWidth = params . maxWidth , // Lines created by manual line breaks (\n) manualLines = allText . split ( '\n' ) , // All lines created manually and by wrapping allLines = [ ] , // Other variables lines , line , l , text , words , w ; // Loop through manually-broken lines for ( l = 0 ; l < manualLines . length ; l += 1 ) { text = manualLines [ l ] ; // Split line into list of words words = text . split ( ' ' ) ; lines = [ ] ; line = '' ; // If text is short enough initially // Or, if the text consists of only one word if ( words . length === 1 || ctx . measureText ( text ) . width < maxWidth ) { // No need to wrap text lines = [ text ] ; } else { // Wrap lines for ( w = 0 ; w < words . length ; w += 1 ) { // Once line gets too wide, push word to next line if ( ctx . measureText ( line + words [ w ] ) . width > maxWidth ) { // This check prevents empty lines from being created if ( line !== '' ) { lines . push ( line ) ; } // Start new line and repeat process line = '' ; } // Add words to line until the line is too wide line += words [ w ] ; // Do not add a space after the last word if ( w !== ( words . length - 1 ) ) { line += ' ' ; } } // The last word should always be pushed lines . push ( line ) ; } // Remove extra space at the end of each line allLines = allLines . concat ( lines . join ( '\n' ) . replace ( / ((\n))|($) / gi , '$2' ) . split ( '\n' ) ) ; } return allLines ; }
Wraps a string of text within a defined width
Wraps a string of text within a defined width