Skip to content

Instantly share code, notes, and snippets.

@buzzdecafe
Last active December 28, 2015 14:29
Show Gist options
  • Save buzzdecafe/7515480 to your computer and use it in GitHub Desktop.
Save buzzdecafe/7515480 to your computer and use it in GitHub Desktop.
pass pointer events through an svg to its parent element
<!doctype html>
<html>
<head>
<style>
#wrapper {
height: 200px;
padding: 20px;
background-color: #ddeeff;
}
#vector {
pointer-events: none;
}
#output {
margin-top: 20px;
border: 1px solid red;
height: 200px;
overflow-y: scroll;
}
</style>
</head>
<body>
<h1>SVG pointer-events</h1>
<div id="wrapper">
<svg xmlns="http://www.w3.org/2000/svg" id="vector">
<rect x="0" y="0" width="100" height="200" fill="red"/>
</svg>
</div>
<div id="output"></div>
<button id="clr">Clear</button>
<script>
(function() {
var out = document.querySelector('#output');
var wrapper = document.querySelector('#wrapper');
function start(e) {
wrapper.addEventListener('mousemove', log);
}
function stop(e) {
wrapper.removeEventListener('mousemove', log);
}
function log(e) {
out.innerHTML += '. ';
}
wrapper.addEventListener('mousedown', start);
wrapper.addEventListener('mouseup', stop);
wrapper.addEventListener('mouseout', stop);
document.querySelector('#clr').addEventListener('click', function(e) { out.innerHTML = ''; });
}());
</script>
</body>
</html>
<!doctype html>
<!-- see this in action: http://jsfiddle.net/HGWLX/2/ -->
<html>
<head>
<style>
#wrapper {
height: 200px;
padding: 20px;
background-color: #ddeeff;
position: relative;
}
#x {
outline: 1px solid red;
width: 100%;
height: 200px;
}
#vector {
pointer-events: none;
position: absolute;
top: 20px;
left: 100px;
}
#output {
margin-top: 20px;
border: 1px solid red;
height: 200px;
overflow-y: scroll;
}
</style>
</head>
<body>
<h1>SVG pointer-events</h1>
<div id="wrapper">
<div id="x"></div>
<svg xmlns="http://www.w3.org/2000/svg" id="vector">
<rect x="0" y="0" width="100" height="200" fill="red"/>
</svg>
</div>
<div id="output"></div>
<button id="clr">Clear</button>
<script>
(function() {
var out = document.querySelector('#output');
var x = document.querySelector('#x');
function start(e) {
x.addEventListener('mousemove', log);
}
function stop(e) {
x.removeEventListener('mousemove', log);
}
function log(e) {
out.innerHTML += '. ';
}
x.addEventListener('mousedown', start);
x.addEventListener('mouseup', stop);
x.addEventListener('mouseout', stop);
document.querySelector('#clr').addEventListener('click', function(e) { out.innerHTML = ''; });
}());
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment