Skip to content

Instantly share code, notes, and snippets.

@modster
Created November 9, 2022 08:26
Show Gist options
  • Save modster/9223b5b2cc68e23ed03c2edd47d6f8d2 to your computer and use it in GitHub Desktop.
Save modster/9223b5b2cc68e23ed03c2edd47d6f8d2 to your computer and use it in GitHub Desktop.
Zdog - no Illustration, SVG
<svg class="zdog-svg" width="240" height="240"></svg>
// Made with Zdog
// ----- setup ----- //
// get svg element
let svg = document.querySelector('svg');
// rendering sizes
const zoom = 4;
const sceneWidth = 16;
const sceneHeight = 16;
let viewWidth = sceneWidth * zoom;
let viewHeight = sceneHeight * zoom;
let svgWidth = svg.getAttribute('width');
let svgHeight = svg.getAttribute('height');
// set viewBox using zoom
svg.setAttribute( 'viewBox', `${-viewWidth/2} ${-viewHeight/2} ` +
`${viewWidth} ${viewHeight}` );
// rendering variable
let isSpinning = true;
const TAU = Zdog.TAU;
// create an scene Anchor to hold all items
let scene = new Zdog.Anchor();
// ----- model ----- //
// add shapes to scene
new Zdog.Shape({
addTo: scene,
path: [
{ x: 0, y: -8 },
{ x: 8, y: 8 },
{ x: -8, y: 8 },
],
translate: { z: 10 },
color: '#E62',
stroke: 3,
fill: true,
});
new Zdog.Ellipse({
addTo: scene,
diameter: 20,
translate: { z: -10 },
stroke: 5,
color: '#636',
});
// ----- animate ----- //
function animate() {
// make changes to model, like rotating scene
scene.rotate.y += isSpinning ? 0.03 : 0;
scene.updateGraph();
render();
requestAnimationFrame( animate );
}
function render() {
empty( svg );
scene.renderGraphSvg( svg );
}
animate();
function empty( element ) {
while ( element.firstChild ) {
element.removeChild( element.firstChild );
}
}
// ----- drag ----- //
let dragStartRX, dragStartRY;
let minSize = Math.min( svgWidth, svgHeight );
// add drag-rotatation with Dragger
new Zdog.Dragger({
startElement: svg,
onDragStart: function() {
isSpinning = false;
dragStartRX = scene.rotate.x;
dragStartRY = scene.rotate.y;
},
onDragMove: function( pointer, moveX, moveY ) {
scene.rotate.x = dragStartRX - ( moveY / minSize * TAU );
scene.rotate.y = dragStartRY - ( moveX / minSize * TAU );
},
});
<script src="https://unpkg.com/zdog@1/dist/zdog.dist.js"></script>
.zdog-svg {
background: #FDB;
cursor: move;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment