From 1bf607390fd2ddc9378c3a77a91df7d1ff1b98d1 Mon Sep 17 00:00:00 2001 From: Vadim Namniak Date: Sun, 12 Oct 2014 01:55:46 -0400 Subject: [PATCH] added stroke text --- index.html | 10 +++++++++- js/CanvasTextWrapper.js | 9 ++++++++- js/examples.js | 15 ++++++++++++--- js/options.js | 14 +++++++------- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/index.html b/index.html index 993f88f..82fad33 100644 --- a/index.html +++ b/index.html @@ -82,7 +82,15 @@ (Boolean) - auto font size to fill text container + +
  • +
    strokeText
    + (Boolean) - text outline based on context configuration (make sure it doesn't contradict with other context settings such as globalCompositeOperation, etc) +
  • diff --git a/js/CanvasTextWrapper.js b/js/CanvasTextWrapper.js index 97b04f3..60a7fdf 100644 --- a/js/CanvasTextWrapper.js +++ b/js/CanvasTextWrapper.js @@ -16,7 +16,8 @@ paddingY: 0, // zero px top & bottom text padding relative to canvas or parent fitParent: false, // text is tested to fit canvas width lineBreak: 'auto', // text fills the element's (canvas or parent) width going to a new line on a whole word - sizeToFill: false // text is resized to fill the container (given font size is ignored) + sizeToFill: false, // text is resized to fill the container (given font size is ignored) + strokeText: false // text is stroked according to context configuration. }; window.CanvasTextWrapper = function(canvas, text, opts) { @@ -82,6 +83,9 @@ textPos.y = parseInt(textPos.y) + parseInt(this.lineHeight); this.context.fillText(lines[i], textPos.x, textPos.y); + if (this.strokeText) { + this.context.strokeText(lines[i], textPos.x, textPos.y); + } } }, @@ -194,6 +198,9 @@ if (typeof this.sizeToFill !== 'boolean') { throw new TypeError('From CanvasTextWrapper(): Property "sizeToFill" must be set to a Boolean.'); } + if (typeof this.strokeText !== 'boolean') { + throw new TypeError('From CanvasTextWrapper(): Property "strokeText" must be set to a Boolean.'); + } } }; })(); \ No newline at end of file diff --git a/js/examples.js b/js/examples.js index ccd8f75..36f91c5 100644 --- a/js/examples.js +++ b/js/examples.js @@ -43,9 +43,18 @@ document.onreadystatechange = function() { context.fillRect(0, 0, w, h); exampleItem.appendChild(canvasMask); - // create text to be cut out mask layer - context.fillStyle = '#212121'; - context.globalCompositeOperation = 'destination-out'; + if (i < options.length -1) { + // create text to be cut out mask layer + context.fillStyle = '#212121'; + context.globalCompositeOperation = 'destination-out'; + } else { + // make stroke gradient + var gradient=context.createLinearGradient(0,0,canvasImg.width,0); + gradient.addColorStop("0","#ffff00"); + gradient.addColorStop("1.0","red"); + context.strokeStyle=gradient; + context.lineWidth = 3; + } // create wrapper new CanvasTextWrapper(canvasMask, ('#' + (i + 1) + ' ' + text), options[i]); diff --git a/js/options.js b/js/options.js index aa4f77e..3599f4e 100644 --- a/js/options.js +++ b/js/options.js @@ -21,16 +21,16 @@ verticalAlign: 'middle' }, { + textAlign: 'center', + verticalAlign: 'middle', + sizeToFill: true + }, + { font: 'bold 50px Tahoma, Geneva, sans-serif', textAlign: 'right', paddingX: 25, - paddingY: 25 + paddingY: 25, + strokeText: true }, - { - textAlign: 'center', - verticalAlign: 'middle', - //lineBreak: 'word', - sizeToFill: true - } ]; })(); \ No newline at end of file