forked from wentin/underlineJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
single underline pixel perfect works with ratio
- Loading branch information
Showing
7 changed files
with
141 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.DS_Store | ||
**/.sass-cache | ||
**/.sass-cache/* |
Binary file modified
BIN
+42 Bytes
(100%)
css/.sass-cache/342bac2508c9890b8fdf2734444a94b11420b958/animation.scssc
Binary file not shown.
Binary file modified
BIN
+42 Bytes
(100%)
css/.sass-cache/342bac2508c9890b8fdf2734444a94b11420b958/reset.scssc
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
css/.sass-cache/342bac2508c9890b8fdf2734444a94b11420b958/stylesheet.scssc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
var multiplyValue = function(value, multiplier){ | ||
var str = value; | ||
var m = multiplier; | ||
var result = str.match(/(\d*\.?\d*)(.*)/); | ||
//http://stackoverflow.com/questions/2868947/split1px-into-1px-1-px-in-javascript | ||
return result[1] * m + result[2]; | ||
} | ||
|
||
var optimalStrokeWidthPos = function(strokeWidth, posY){ | ||
if ( strokeWidth < 1) { | ||
posY = Math.round(posY - 0.5) + 0.5; | ||
} else if ( strokeWidth >= 1 ) { | ||
strokeWidth = Math.round( strokeWidth ); | ||
if ( strokeWidth % 2 ){ | ||
// odd, posY -> 0.5 | ||
posY = Math.round(posY - 0.5) + 0.5; | ||
} else { | ||
// even, posY -> 1 | ||
posY = Math.round(posY); | ||
} | ||
} | ||
return { | ||
strokeWidth: strokeWidth, | ||
posY: posY | ||
} | ||
} | ||
|
||
function SingleUnderline(element, underlineStyles, elementStyles) { | ||
//ctor | ||
this.element = element; | ||
|
||
this.text = this.element.textContent; | ||
|
||
this.underlineStyles = underlineStyles; | ||
|
||
this.elementStyles = elementStyles; | ||
this.redrawActive = false; | ||
|
||
this.canvas = document.createElement("canvas"); | ||
this.ctx = this.canvas.getContext('2d'); | ||
|
||
this.ratio = window.devicePixelRatio; | ||
this.canvas.width = this.elementStyles.width*this.ratio; | ||
this.canvas.height = this.elementStyles.height*this.ratio; | ||
this.element.appendChild(this.canvas); | ||
this.canvas.style.width = this.elementStyles.width + 'px'; | ||
|
||
this.ctx.font = this.font = this.elementStyles.fontStyle + ' ' | ||
+ multiplyValue(this.elementStyles.fontSize, this.ratio) + ' ' | ||
+ this.elementStyles.fontFamily; | ||
|
||
// determine the text-underline-width / strokeWidth | ||
dotWidth = this.ctx.measureText('.')['width']; | ||
if (this.underlineStyles['text-underline-width'] == "auto") { | ||
// if set to auto, calculate the optimized width based on font | ||
this.strokeWidth = dotWidth/6; | ||
} else { | ||
//if set to px value, todo: other unit such as em? | ||
this.strokeWidth = this.underlineStyles['text-underline-width']; | ||
//get number value | ||
this.strokeWidth = parseFloat(this.strokeWidth)*this.ratio; | ||
} | ||
|
||
|
||
// determine the text-underline-position / underlinePosition | ||
// text-underline-position in ratio, todo: default and user set position ratio | ||
this.underlinePosition = parseFloat(this.elementStyles.height) * this.ratio * | ||
(1 - this.elementStyles.baselinePositionRatio | ||
+ this.underlineStyles['text-underline-position']); | ||
|
||
var adjustValue = optimalStrokeWidthPos(this.strokeWidth, this.underlinePosition); | ||
this.strokeWidth = adjustValue.strokeWidth; | ||
this.underlinePosition = adjustValue.posY; | ||
|
||
this.textWidth = this.ctx.measureText(this.text).width; | ||
|
||
this.myString = new GuitarString(this.ctx, | ||
new Point(0, this.underlinePosition), | ||
new Point(this.textWidth, this.underlinePosition), | ||
this.strokeWidth, this.underlineStyles['text-underline-color'], this.ratio); | ||
this.drawHoles(); | ||
|
||
} | ||
|
||
SingleUnderline.prototype.clear = function(){ | ||
this.redrawActive = this.myString.redrawActive; | ||
// clear | ||
if(this.myString.redrawActive) { | ||
// this.myString.clear(); | ||
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height); | ||
} | ||
}; | ||
|
||
|
||
SingleUnderline.prototype.update = function(){ | ||
// update | ||
if(this.myString.redrawActive) { | ||
this.myString.update(); | ||
// this.drawHoles(); | ||
} | ||
}; | ||
|
||
|
||
SingleUnderline.prototype.draw = function(){ | ||
// draw | ||
if(this.redrawActive) { | ||
this.drawUnderline(); | ||
this.drawHoles(); | ||
} | ||
}; | ||
|
||
SingleUnderline.prototype.drawUnderline = function(){ | ||
// draw the underline | ||
this.myString.draw(); | ||
} | ||
|
||
SingleUnderline.prototype.drawHoles = function(){ | ||
|
||
// draw the font stroke | ||
this.ctx.font = this.font; | ||
this.ctx.textBaseline = 'top'; | ||
|
||
this.ctx.globalCompositeOperation = "destination-out"; | ||
|
||
this.ctx.lineWidth = 2*this.ratio + this.strokeWidth*2; | ||
console.log(this.ctx.lineWidth); | ||
this.ctx.strokeStyle = 'blue'; | ||
this.ctx.beginPath(); | ||
this.ctx.strokeText(this.text, 0, 0); | ||
|
||
this.ctx.fillStyle = 'green'; | ||
this.ctx.beginPath(); | ||
this.ctx.fillText(this.text, -1, 0); | ||
} | ||
|