Skip to content

Commit

Permalink
Scale sizes up on phones (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidavidm committed May 6, 2018
1 parent 79b7290 commit 2cb9ab6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/gfx/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,8 @@ import * as custom from "./custom";
import * as layout from "./layout";
import * as shapes from "./shapes";
import * as ui from "./ui";
import * as viewport from "./viewport";

export { default as decal } from "./decal";
export * from "./sprite";
export { custom, layout, primitive, shapes, ui };
export { custom, layout, primitive, shapes, ui, viewport };
4 changes: 4 additions & 0 deletions src/gfx/viewport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const IS_PHONE = window.matchMedia("only screen and (max-device-width: 812px) and (-webkit-min-device-pixel-ratio: 1.5)").matches;
export const IS_TABLET = window.matchMedia("only screen and (max-device-width: 1366px) and (-webkit-min-device-pixel-ratio: 1.5)").matches;

export const IS_MOBILE = IS_PHONE || IS_TABLET;
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ function initialize() {
canvas = document.createElement("canvas");
document.body.appendChild(canvas);

if (gfx.viewport.IS_PHONE) {
canvas.style.width = "100%";
}

// Reducer needs access to the views in order to save their state
// for undo/redo.
const reduct = reducer.reduct(es6, views, (id, { x, y }) => {
Expand Down
16 changes: 8 additions & 8 deletions src/stage/basestage.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ export default class BaseStage {
computeDimensions() {
this.ctx.scale(1.0, 1.0);
this._height = window.innerHeight - 40;
if (window.matchMedia("only screen and (max-device-width: 812px) and (-webkit-min-device-pixel-ratio: 1.5)").matches) {
this._width = window.innerWidth;
if (gfxCore.viewport.IS_PHONE) {
this._width = window.innerWidth * 0.75;
this._height *= 0.75;
}
else if (window.matchMedia("only screen and (max-device-width: 1366px) and (-webkit-min-device-pixel-ratio: 1.5)").matches) {
else if (gfxCore.viewport.IS_TABLET) {
this._width = window.innerWidth;
}
else {
Expand Down Expand Up @@ -104,11 +105,10 @@ export default class BaseStage {

getMousePos(e) {
const rect = this.canvas.getBoundingClientRect();
return {
// TODO: scale
x: (e.clientX - rect.left),
y: (e.clientY - rect.top),
};
// Scaling
const x = ((e.clientX - rect.left) / rect.width) * this._width;
const y = ((e.clientY - rect.top) / rect.height) * this.height;
return { x, y };
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/stage/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,9 +440,7 @@ export default class Stage extends BaseStage {
}

getMousePos(e) {
const rect = this.canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const { x, y } = super.getMousePos(e);

return { x: x - this.sidebarWidth, y, sidebar: x - this.sidebarWidth < 0 };
}
Expand Down

0 comments on commit 2cb9ab6

Please sign in to comment.