-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modify TinySDF to return only alpha channel #3
Conversation
@mourner I tacked on an additional 'font-weight' parameter (motivated by Mapbox GL JS use case of using a single font family for Chinese characters but changing weights based on the style). |
index.js
Outdated
this.radius = radius || 8; | ||
var size = this.size = this.fontSize + this.buffer * 2; | ||
|
||
this.canvas = document.createElement('canvas'); | ||
this.canvas.width = this.canvas.height = size; | ||
|
||
this.ctx = this.canvas.getContext('2d'); | ||
this.ctx.font = fontSize + 'px ' + this.fontFamily; | ||
/* style | variant | weight | size/line-height | family */ | ||
this.ctx.font = `normal normal ${this.fontWeight} ${fontSize}px/normal ${this.fontFamily}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't yet use ES6 in this module so let's keep the concatenation. I think the shorthand form can be simplified too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I switched to the format 'font-weight font-size font-family'. I read the "Shorthand font property: the 'font' property" section of the CSS spec, and it seems to me to be a little ambiguous about how it distinguishes 'font-weight'/'font-variant'/'font-style'. But I guess it can disambiguate based on the value (except for 'normal' vs 'inherited').
index.js
Outdated
@@ -37,10 +39,10 @@ TinySDF.prototype.draw = function (char) { | |||
this.ctx.fillText(char, this.buffer, this.middle); | |||
|
|||
var imgData = this.ctx.getImageData(0, 0, this.size, this.size); | |||
var data = imgData.data; | |||
var alphaChannel = new Uint8Array(this.size * this.size); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use Uint8ClampedArray
here instead of Uint8Array
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
f73e27f
to
52871d7
Compare
Update demo to convert the alpha channel back to an ImageData.
52871d7
to
a9d8ad6
Compare
It's convenient for the demo to use an RGBA
ImageData
for building the texture, but in general I think we'd expect users of TinySDF to work with an alpha-channel-only result since that's the only actual information in the SDF we generate.I updated the demo to transform the alpha channel into an
ImageData
, and didn't see any noticeable change in performance.