├── example.png ├── LICENSE ├── README.md ├── index.html └── ctxtextpath.js /example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Viglino/Canvas-TextPath/HEAD/example.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Jean-Marc Viglino 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Canvas-TextPath](https://github.com/Viglino/Canvas-TextPath/) 2 | 3 | Adds extra functionality to the CanvasRenderingContext2D to draw text along a path. 4 | 5 |  6 | 7 | Check out the [demo!](https://viglino.github.io/Canvas-TextPath/) 8 | 9 | ## How it runs? 10 | 11 | As SVG does support text along paths, canvas doesn't have such native support. 12 | This contribution tries to fill the lack and gives you a `textPath` function to use with a CanvasRenderingContext2D. 13 | The method writes one character at a time rotated and scaled according to the path. 14 | 15 | NOTE: this extends the CanvasRenderingContext2D prototype. 16 | - It adds a `textPath` function to draw text along a path (as an Array of coordinates). 17 | - It adds `textOverflow`, `textJustify` and `textStrokeMin` properties to CanvasRenderingContext2D. 18 | 19 | It support native options include text alignment (left, center and right) and baseline positionning. 20 | Stroke and fill is done in one pass for performance purpose. 21 | 22 | 23 | ## Usage 24 | 25 | Include the following files in your page: 26 | ```html 27 | 28 | ``` 29 | Add a canvas on your page 30 | ```html 31 | 32 | ``` 33 | Then begin to draw in the canvas 34 | ```javascript 35 | window.onload = function() { 36 | var c = document.getElementById("myCanvas"); 37 | var ctx = c.getContext("2d"); 38 | 39 | // Draw support path 40 | ctx.strokeStyle = "red"; 41 | ctx.lineWidth=2; 42 | ctx.moveTo(10,60); 43 | ctx.lineTo(100,40); 44 | ctx.lineTo(190,60); 45 | ctx.stroke(); 46 | 47 | // Render text 48 | ctx.font = "24px Arial"; 49 | ctx.textAlign = "center"; 50 | ctx.textBaseline = "middle"; 51 | ctx.strokeStyle = "white"; 52 | ctx.lineWidth = 3; 53 | ctx.textPath ("Hello world", [10,60, 100,40, 190,60]); 54 | } 55 | 56 | ``` 57 | The path is an Array of coordinates [x1,y1, x2, y2, etc.] 58 | 59 |  60 | 61 | ## Properties 62 | 63 | Extra properties are added to the CanvasRenderingContext2D. 64 | 65 | * `textOverflow` string: specifies what happens if text overflows the path, default "" (means hidden). Possible values are 'visible' to show the text, 'ellipsis' to show "..." or a string that will be displayed at the end of the truncated text. Use an empy string to hide overflow content. 66 | * `textJustify` boolean: true for justifying text, default false. When false, it takes the textAlign propertie to align text. 67 | * `textStrokeMin` number: the minimum size (in pixel) of the path underneath the text is not displayed. 68 | 69 | If you specify a `lineWitdth` less than 0.1 no stroke is drawn. Use a `fillStyle` as 'transparent' to not fill the text 70 | 71 | ```javascript 72 | ctx.textOverflow = "ellipsis"; 73 | ctx.textJustify = true; 74 | ctx.textStrokeMin = 40; 75 | ctx.textPath ("Hello world", [10,60, 100,40, 190,60]); 76 | ``` 77 | 78 | 79 | ## License 80 | 81 | [MIT License](https://github.com/Viglino/Canvas-TextPath/blob/master/LICENSE) 82 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |
23 |
25 | To render text along a path in a Canvas.
26 | It adds extra functionality to the CanvasRenderingContext2D by extending its prototype.
27 |
28 | Text:
29 |