Skip to content

Instantly share code, notes, and snippets.

@baldur
Created August 29, 2014 19:43
Show Gist options
  • Save baldur/9bb2264ac1a8d403749c to your computer and use it in GitHub Desktop.
Save baldur/9bb2264ac1a8d403749c to your computer and use it in GitHub Desktop.
with_canvas
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en-us">
<head>
<title>Canvas stuff</title>
<style>
html, body { width: 100%; height: 100%; margin: 0px; }
</style>
</head>
<body>
<canvas id="gallery"></canvas>
</body>
<script type="text/javascript">
var canvas = document.getElementById('gallery');
if (canvas.getContext) {
var context = canvas.getContext('2d');
context.canvas.width = window.innerWidth;
context.canvas.height = 300;
var image = new Image();
image.onload = function() {
context.save();
context.beginPath();
context.moveTo(0, 0);
context.moveTo(0, 80);
context.lineTo(window.innerWidth/2, 180);
context.lineTo(window.innerWidth, 80);
context.lineTo(window.innerWidth, 0);
context.lineTo(0, 0);
context.closePath();
context.clip();
context.drawImage(image, 0, 0, window.innerWidth, image.height * (window.innerWidth/image.width));
context.restore();
};
image.src = 'https://staging.mapzen.com/images/nyc-sunset.png';
context.beginPath();
context.moveTo(0, 100);
context.lineWidth = 2;
context.lineTo(window.innerWidth/2, 200);
context.lineTo(window.innerWidth, 100);
context.stroke();
}
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment