Skip to content

Instantly share code, notes, and snippets.

@tyrasd
Created December 2, 2012 10:19
Show Gist options
  • Save tyrasd/4188058 to your computer and use it in GitHub Desktop.
Save tyrasd/4188058 to your computer and use it in GitHub Desktop.
Canvas floating point precision underflow - used to be a showcase for https://github.com/systemed/iD/issues/163 - keeping as a scrubby example usecase for me
<!doctype html>
<title>Scrubby</title>
<script src='http://nornagon.github.com/scrubby/scrubby.all.js'></script>
<style>
body { text-align: center; margin: 0; }
canvas {
box-shadow: 1px 1px 6px rgba(0,0,0,0.4);
}
#code {
text-align: left;
-webkit-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
}
.canvas {
-webkit-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
width:500px;
}
#container {
width: 100%;
display: -webkit-flex;
display: -moz-box;
display: flex;
-webkit-flex-direction: row;
-webkit-align-items: center;
margin: auto;
}
#containercontainer {
width: 100%;
background: rgb(248, 248, 248);
padding-top: 15px;
padding-bottom: 15px;
box-shadow: 0px 1px 7px rgba(0,0,0,0.2) inset;
}
.scrubby-files {
display: none;
}
pre {
font-family: Consolas, 'Liberation Mono', Courier, monospace;
text-shadow: 0px 1px 0px white;
font-size: 12pt;
line-height: 1.2em;
}
h1 {
font-family: 'HVDComicSerifProRegular';
text-transform: uppercase;
font-size: 36pt;
line-height: 0.9em;
text-shadow: 0px 1px 0px hsl(91, 43%, 63%), 0px 2px 0px hsl(91, 43%, 73%), 0px 3px 0px hsl(91, 43%, 83%), 0px 3px 1px hsla(0, 0%, 0%, 0.2), 0px 0px 10px hsla(0, 0%, 0%, 0.3);
}
html {
font-family: Helvetica, arial, sans-serif;
}
p {
font-family: georgia;
text-shadow: 0px 1px 0px rgba(0,0,0,0.3);
max-width: 800px;
margin-left: auto;
margin-right: auto;
text-align: left;
}
code {
font-family: Consolas, 'Liberation Mono', Courier, monospace;
margin: 0 2px;
padding: 0px 5px;
border: 1px solid hsl(0, 0%, 92%);
background-color: hsl(0, 0%, 97%);
border-radius: 3px;
white-space: nowrap;
text-shadow: none;
}
span.scrub {
font-weight: bold;
background-color: yellow;
}
</style>
<div id='containercontainer'>
<div id='container'>
<div class='canvas'>
<canvas id='canvas'></canvas>
</div>
<div id='code'></div>
</div>
</div>
<p>
Based on an example for <a href="https://github.com/nornagon/scrubby">scrubby</a> by Jeremy Apthorp.
</p>
<script type='text/scrubby'>function draw() {
canvas.width = canvas.height = '400';
ctx = canvas.getContext('2d');
ctx.fillStyle = 'red';
ctx.translate('30', '30')
ctx.save()
ctx.fillRect('0', '0', '100', '100')
ctx.fillStyle = "blue";
var large_offset = Math.pow("10",8);
var small_translation = 0;
ctx.translate(-large_offset,'0');
ctx.translate(small_translation,'0');
ctx.fillRect(large_offset, '0', '100', '100')
ctx.restore()
}
</script>
<script>
window.addEventListener('DOMContentLoaded', function() {
draw()
scrubby.on('scrubbed', function() {
draw()
})
var f = scrubby.makeScrubbingFrame('inline#1')
code.appendChild(f)
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment