Skip to content

Commit 150f524

Browse files
committed
Add support for retina (HDPI) displays.
1 parent 36efa6c commit 150f524

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

blockcode/blocks.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ h1{ margin: 0em; }
99
.script{ background-color: LightSteelBlue; height: 94%; overflow-y: auto; }
1010
.script.over{ background-color: CadetBlue; }
1111
.canvas-column{ width: 46%; height: 85%; float: right; margin-right: 10px; }
12-
.canvas{ background-color: white; width: 100%; height: 85%; }
12+
.canvas-placeholder{ width: 100%; height: 85%; }
13+
.canvas{ background-color: white; position: absolute; }
1314
.block{ padding: 5px; margin: 10px; cursor: move; }
1415
.block.running{ outline: 2px solid red; }
1516
.block.next{ outline: 2px solid green; }

blockcode/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ <h2>Examples:
2929
<option value="spiral">Spiral</option>
3030
</select>
3131
</h2>
32+
<div class="canvas-placeholder"></div>
3233
<canvas class="canvas"></canvas>
3334
</div>
3435
<script src="util.js"></script>

blockcode/turtle.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
(function(){
22
'use strict';
33

4+
var PIXEL_RATIO = window.devicePixelRatio || 1;
5+
var canvasPlaceholder = document.querySelector('.canvas-placeholder');
46
var canvas = document.querySelector('.canvas');
57
var script = document.querySelector('.script');
68
var ctx = canvas.getContext('2d');
@@ -9,10 +11,14 @@
911
var WIDTH, HEIGHT, position, direction, visible, pen, color;
1012

1113
function onResize(evt){
12-
canvas.setAttribute('width', canvas.scrollWidth);
13-
canvas.setAttribute('height', canvas.scrollHeight);
14-
WIDTH = canvas.scrollWidth;
15-
HEIGHT = canvas.scrollHeight;
14+
WIDTH = canvasPlaceholder.getBoundingClientRect().width * PIXEL_RATIO;
15+
HEIGHT = canvasPlaceholder.getBoundingClientRect().height * PIXEL_RATIO;
16+
canvas.setAttribute('width', WIDTH);
17+
canvas.setAttribute('height', HEIGHT);
18+
canvas.style.top = canvasPlaceholder.getBoundingClientRect().top + "px";
19+
canvas.style.left = canvasPlaceholder.getBoundingClientRect().left + "px";
20+
canvas.style.width = (WIDTH / PIXEL_RATIO) + "px"
21+
canvas.style.height = (HEIGHT / PIXEL_RATIO) + "px"
1622
if (evt){
1723
Menu.runSoon();
1824
}
@@ -46,8 +52,8 @@
4652
function _moveForward(distance){
4753
var start = position;
4854
position = {
49-
x: cos(direction) * distance + start.x,
50-
y: -sin(direction) * distance + start.y
55+
x: cos(direction) * distance * PIXEL_RATIO + start.x,
56+
y: -sin(direction) * distance * PIXEL_RATIO + start.y
5157
};
5258
if (pen){
5359
ctx.lineStyle = color;

0 commit comments

Comments
 (0)