I am developing a browser game with a client-server architecture. The game involves an HTML canvas as the playground, and I would like to be able to set the dimensions of this canvas in a single config file, and then reuse it 1) in the CSS to define the actual dimensions of the canvas and 2) in the code of the game server for collisions and stuff.
What is the best way to do this? I think I have to use a CSS preprocessor (Sass or Less), but how can I import variables from JS or JSON files into Sass?
Sorry for bad spanish :)
A bit late answer, but here it is (at least for other readers): css preprocessor will probably be necessary.
My favorite way to go is node-sass-json-importer.
First, a bit of configuration
You need to equip yourself with node-sass (but if you're actually using both node and sass, you've probably done that already). You specify the
importer
option in yournode-sass
file:or if you're using webpack with sass-loader (my preferred way):
Now the actual usage
You just define shared variables in a json file, like this:
To use it in js, well... it's JSON ;) To use in SASS:
THAT easy ;)
The other options is the following: if you opt for one of those: Stylus, Sass or LESS, you can use Rosetta. With that, you specify the shared data in one file like:
compile the file like this:
You reference the generated files in the standard way: