Skip to content

Instantly share code, notes, and snippets.

@derpycoder
Last active March 18, 2020 10:37
Show Gist options
  • Save derpycoder/1248690d8ec980f913473a9de825e350 to your computer and use it in GitHub Desktop.
Save derpycoder/1248690d8ec980f913473a9de825e350 to your computer and use it in GitHub Desktop.
CSS Pattern Background & Pan Using Mouse
Display the source blob
Display the rendered blob
Raw
<svg xmlns='http://www.w3.org/2000/svg' width='128' height='128'>
<rect width='128' height='128' fill='#1a5ac2'/>
<g fill='#3576c9'>
<rect width='128' height='1' y='25.6'/>
<rect width='128' height='1' y='51.2'/>
<rect width='128' height='1' y='76.8'/>
<rect width='128' height='1' y='102.4'/>
<rect width='1' height='128' x='25.6'/>
<rect width='1' height='128' x='51.2'/>
<rect width='1' height='128' x='76.8'/>
<rect width='1' height='128' x='102.4'/>
</g>
<rect width='128' height='128' fill='none' stroke-width='2' stroke='#749cda'/>
</svg>
<html>
<head>
<script>
function init() {
const TOPOGRAPHY_PAT = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiBjbGFzcz0ic3ZlbHRlLTFrbXJ1MHUiPjxyZWN0IHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiBmaWxsPSIjZmZmIj48L3JlY3Q+PGc+PGNpcmNsZSBjeD0iMCIgY3k9IjAiIHI9IjIiIGZpbGw9IiNjY2MiPjwvY2lyY2xlPjxjaXJjbGUgY3g9IjAiIGN5PSIzMiIgcj0iMSIgZmlsbD0iI2NjYyI+PC9jaXJjbGU+PGNpcmNsZSBjeD0iMCIgY3k9IjY0IiByPSIxIiBmaWxsPSIjY2NjIj48L2NpcmNsZT48Y2lyY2xlIGN4PSIwIiBjeT0iOTYiIHI9IjEiIGZpbGw9IiNjY2MiPjwvY2lyY2xlPjxjaXJjbGUgY3g9IjAiIGN5PSIxMjgiIHI9IjIiIGZpbGw9IiNjY2MiPjwvY2lyY2xlPjxjaX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jxyZWN0IHdpZHRoPSIxIiBoZWlnaHQ9IjEyOCIgeD0iMTAyLjQiPjwvcmVjdD48L2c+PHJlY3Qgd2lkdGg9IjEyOCIgaGVpZ2h0PSIxMjgiIGZpbGw9Im5vbmUiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlPSIjNzQ5Y2RhIj48L3JlY3Q+PC9zdmc+";
const HATCH_PAT = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxNCIgaGVpZ2h0PSIxNCI+CiAgPHBhdGggZD0iTS0xLDEgbDcsLTcgTTAsMTQgbDE0LC0xNCBNMTMsMTUgbDcsLTciIHN0cm9rZT0id2hpdGUiIHN0cm9rZS13aWR0aD0iMSI+PC9wYXRoPgogIDxyZWN0IHdpZHRoPSIxNCIgaGVpZ2h0PSIxNCIgc3Ryb2tlPSJub25lIiBmaWxsPSJ3aGl0ZSIgb3BhY2l0eT0iMC4yIj4KPC9yZWN0Pjwvc3ZnPg==";
const target = document.getElementById("target");
const backgroundDelta = { x: 0, y: 0 };
target.style.background = `url(${TOPOGRAPHY_PAT})`;
target.addEventListener("mousedown", (event) => {
target.addEventListener("mousemove", handleDrag);
});
target.addEventListener("mouseup", (event) => {
target.removeEventListener("mousemove", handleDrag);
});
function handleDrag(event) {
backgroundDelta.x += event.movementX;
backgroundDelta.y += event.movementY;
target.style.backgroundPositionX = `${backgroundDelta.x}px`;
target.style.backgroundPositionY = `${backgroundDelta.y}px`;
}
}
</script>
<style>
*,
*:before,
*:after {
box-sizing: inherit;
}
html,
body {
margin: 0;
padding: 0 !important;
overflow: hidden;
box-sizing: border-box;
}
#target {
width: 100%;
height: 100vh;
}
</style>
</head>
<body onLoad="init()">
<div id="target"></div>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
function svgToBase64Image() {
const svg = document.getElementById('target-svg');
const div = document.createElement('div');
div.appendChild(svgElement.cloneNode(true));
const b64 = window.btoa(div.innerHTML);
return `data:image/svg+xml;base64,${b64}`;
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment