CesiumGS / cesium

An open-source JavaScript library for world-class 3D globes and maps :earth_americas:
https://cesium.com/cesiumjs/
Apache License 2.0
12.95k stars 3.49k forks source link

Save 9.71 kb minified by removing 147 static named Colors #8258

Open puckey opened 5 years ago

puckey commented 5 years ago

I noticed 147 named static colors located in Core/Color.js. Removing them saved 9,71kb minified in bundle size.

Aside from one or two uses in the core library, they seem to only be used in sandcastle examples and specs, leading me to believe they could do with being extracted or adapted.

An adaption could be to extend Color.fromCssColorString('string') to also accept color names: instead of using Color.PINK we could have Color.fromCssColorString('pink').

In Color.fromCssColorString you could get the color through a canvas object and cache it by name. We did this in Paper.js and it works well: https://github.com/paperjs/paper.js/blob/e5d7bafd39db546e6c78bdbd89212c1685e05e1b/src/style/Color.js#L111

Color.fromCssColorString could also be shortened to Color.fromCss..

puckey commented 5 years ago

Another approach would be to limit the used colors in the spec & sandcastle demos to a smaller set of 16 instead of 147.

maxdow commented 5 years ago

Color.js in Cesium is an helper but yes , it grows the lib . It can be a good idea to use Color.js as an addon.

Is the use of the canvas not problematic for performance ?

puckey commented 5 years ago

Performance is ok.. But I would go for keeping a small subset of this huge list.