|
45 | 45 | animate: true
|
46 | 46 | };
|
47 | 47 | settings = Object.assign(defaults, settings);
|
48 |
| - let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _animated = settings.animate, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 18, _rng_seed = Date.now(), _global = settings.global, _events = { |
| 48 | + let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _animated = settings.animate, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rng_seed = Date.now(), _global = settings.global, _events = { |
49 | 49 | init: null,
|
50 | 50 | update: null,
|
51 | 51 | draw: null,
|
|
902 | 902 | root[key] = value;
|
903 | 903 | }
|
904 | 904 | },
|
905 |
| - /** |
906 |
| - * Resizes the game canvas and emit the "resized" event |
907 |
| - * |
908 |
| - * @param {number} width |
909 |
| - * @param {number} height |
910 |
| - */ |
911 |
| - resize(width, height) { |
912 |
| - DEV: assert( |
913 |
| - isFinite(width) && width > 0, |
914 |
| - "resize: 1st param must be a number" |
915 |
| - ); |
916 |
| - DEV: assert( |
917 |
| - isFinite(height) && height > 0, |
918 |
| - "resize: 2nd param must be a number" |
919 |
| - ); |
920 |
| - instance.setvar("WIDTH", _canvas.width = width); |
921 |
| - instance.setvar("HEIGHT", _canvas.height = height); |
922 |
| - instance.setvar("CENTERX", instance.WIDTH / 2); |
923 |
| - instance.setvar("CENTERY", instance.HEIGHT / 2); |
924 |
| - onResize(); |
925 |
| - }, |
926 | 905 | /**
|
927 | 906 | * The scale of the game's delta time (dt).
|
928 | 907 | * Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
|
|
980 | 959 | loadPlugin(callback, config);
|
981 | 960 | }
|
982 | 961 | if (settings.autoscale) {
|
983 |
| - on(root, "resize", onResize); |
| 962 | + on(root, "resize", resizeCanvas); |
984 | 963 | }
|
985 | 964 | if (settings.tapEvents) {
|
986 | 965 | const _getXY = (pageX, pageY) => [
|
|
1180 | 1159 | _canvas && _canvas.tagName === "CANVAS",
|
1181 | 1160 | "Invalid canvas element"
|
1182 | 1161 | );
|
1183 |
| - DEV: assert( |
1184 |
| - null == instance.WIDTH || instance.WIDTH > 0, |
1185 |
| - `Litecanvas' "width" option should be null or a positive number` |
1186 |
| - ); |
1187 |
| - DEV: assert( |
1188 |
| - null == instance.HEIGHT || instance.HEIGHT > 0, |
1189 |
| - `Litecanvas' "width" option should be null or a positive number` |
1190 |
| - ); |
1191 |
| - DEV: assert( |
1192 |
| - null == instance.HEIGHT || instance.WIDTH > 0 && instance.HEIGHT > 0, |
1193 |
| - `Litecanvas' "width" is required when "heigth" is passed` |
1194 |
| - ); |
1195 | 1162 | instance.setvar("CANVAS", _canvas);
|
1196 | 1163 | _ctx = _canvas.getContext("2d");
|
1197 | 1164 | on(_canvas, "click", () => root.focus());
|
1198 | 1165 | _canvas.style = "";
|
1199 |
| - if (!instance.WIDTH) { |
1200 |
| - instance.WIDTH = root.innerWidth; |
1201 |
| - instance.HEIGHT = root.innerHeight; |
1202 |
| - } |
1203 |
| - instance.resize(instance.WIDTH, instance.HEIGHT, false); |
| 1166 | + resizeCanvas(); |
1204 | 1167 | if (!_canvas.parentNode) document.body.appendChild(_canvas);
|
1205 | 1168 | }
|
1206 |
| - function onResize() { |
1207 |
| - const styles = _canvas.style; |
| 1169 | + function resizeCanvas() { |
| 1170 | + DEV: assert( |
| 1171 | + null == settings.width || isFinite(settings.width) && settings.width > 0, |
| 1172 | + `Litecanvas' option "width" should be a positive number when defined` |
| 1173 | + ); |
| 1174 | + DEV: assert( |
| 1175 | + null == settings.height || isFinite(settings.height) && settings.height > 0, |
| 1176 | + `Litecanvas' option "height" should be a positive number when defined` |
| 1177 | + ); |
| 1178 | + DEV: assert( |
| 1179 | + null == settings.height || settings.width > 0 && settings.height > 0, |
| 1180 | + `Litecanvas' option "width" is required when the option "height" is defined` |
| 1181 | + ); |
| 1182 | + const width = settings.width || root.innerWidth, height = settings.height || settings.width || root.innerHeight; |
| 1183 | + instance.setvar("WIDTH", _canvas.width = width); |
| 1184 | + instance.setvar("HEIGHT", _canvas.height = height); |
| 1185 | + instance.setvar("CENTERX", instance.WIDTH / 2); |
| 1186 | + instance.setvar("CENTERY", instance.HEIGHT / 2); |
1208 | 1187 | if (settings.autoscale) {
|
1209 |
| - if (!styles.display) { |
1210 |
| - styles.display = "block"; |
1211 |
| - styles.margin = "auto"; |
| 1188 | + if (!_canvas.style.display) { |
| 1189 | + _canvas.style.display = "block"; |
| 1190 | + _canvas.style.margin = "auto"; |
1212 | 1191 | }
|
1213 | 1192 | _scale = math.min(
|
1214 | 1193 | root.innerWidth / instance.WIDTH,
|
1215 | 1194 | root.innerHeight / instance.HEIGHT
|
1216 | 1195 | );
|
1217 | 1196 | _scale = (settings.pixelart ? ~~_scale : _scale) || 1;
|
1218 |
| - styles.width = instance.WIDTH * _scale + "px"; |
1219 |
| - styles.height = instance.HEIGHT * _scale + "px"; |
| 1197 | + _canvas.style.width = instance.WIDTH * _scale + "px"; |
| 1198 | + _canvas.style.height = instance.HEIGHT * _scale + "px"; |
1220 | 1199 | }
|
1221 | 1200 | if (!settings.antialias || settings.pixelart) {
|
1222 | 1201 | _ctx.imageSmoothingEnabled = false;
|
1223 |
| - styles.imageRendering = "pixelated"; |
| 1202 | + _canvas.style.imageRendering = "pixelated"; |
1224 | 1203 | }
|
1225 | 1204 | instance.emit("resized", _scale);
|
1226 | 1205 | if (!_animated) {
|
|
0 commit comments