|
1 | 1 | "use strict"; |
2 | 2 |
|
3 | | -var FontMetrics = require('FontMetrics'); |
| 3 | +var FontMetrics = require('FontMetrics'), |
| 4 | + Document = require('Document'); |
4 | 5 |
|
5 | 6 | /** |
6 | 7 | * Simple plain-text text editor using html5 canvas. |
7 | 8 | * @constructor |
8 | 9 | */ |
9 | | -var CanvasTextEditor = function() { |
| 10 | +var CanvasTextEditor = function(doc) { |
| 11 | + this._document = doc || (new Document); |
10 | 12 | this._metrics = new FontMetrics('"Courier New", Courier, monospace', 14); |
11 | 13 | this._createWrapper(); |
12 | 14 | this._createCanvas(); |
@@ -45,9 +47,20 @@ CanvasTextEditor.prototype._createCanvas = function() { |
45 | 47 | this.context = this.canvas.getContext('2d'); |
46 | 48 | this.resize(640, 480); |
47 | 49 |
|
48 | | - // Placeholder function just to see that it's working |
49 | | - this.context.font = this._metrics.getSize() + 'px ' + this._metrics.getFamily() |
50 | | - this.context.fillText('Test', 0, this._metrics.getBaseline()); |
| 50 | + // For now just very dumb implementation of rendering |
| 51 | + var baselineOffset = this._metrics.getBaseline(), |
| 52 | + lineHeight = this._metrics.getHeight(), |
| 53 | + characterWidth = this._metrics.getWidth(), |
| 54 | + maxHeight = Math.ceil(640 / lineHeight), |
| 55 | + lineCount = this._document.getLineCount(); |
| 56 | + |
| 57 | + if (lineCount < maxHeight) maxHeight = lineCount; |
| 58 | + |
| 59 | + for(var i = 0; i < maxHeight; ++i) { |
| 60 | + this.context.fillText( |
| 61 | + this._document.getLine(i), 2, lineHeight * i + baselineOffset |
| 62 | + ); |
| 63 | + } |
51 | 64 |
|
52 | 65 | this.wrapper.appendChild(this.canvas); |
53 | 66 | }; |
@@ -92,6 +105,8 @@ CanvasTextEditor.prototype.getEl = function() { |
92 | 105 | CanvasTextEditor.prototype.resize = function(width, height) { |
93 | 106 | this.canvas.width = width; |
94 | 107 | this.canvas.height = height; |
| 108 | + // We need to update context settings every time we resize |
| 109 | + this.context.font = this._metrics.getSize() + 'px ' + this._metrics.getFamily(); |
95 | 110 | }; |
96 | 111 |
|
97 | 112 | /** |
|
0 commit comments