// hacky way to set many attributes - just for fun
const el = document.body.appendChild(
document.createElement('button'));
let i, a;
el.innerHTML='hello';
for(i in a ={
id:'xyz',
title:'hi there...',
style: `
background: red;
color:white;
font-size: 1.4em;
`
}) el.setAttribute(i, a[i]);
Chances are you don’t want to use this unless you are just speed coding, prototyping or playing around
If you do want to set multiple attributes, you’ll likely be doing so in an abstract way with one of the many popular js libs or frameworks.
In my personal projects I usually don’t use DOM related libs or frameworks, so I do this or something like it:
function attrs(el, attrs){
for(let i in attrs){
el.setAttribute(i, attrs[i])
}
}
I’ll always miss jQuery, used it for many years and found it extremely intuitive and a huge time saver… The above attrs function is just a crappier version of the jQuery attr method. Read more about that here in the docs.
Move your mouse so that the green rectangle touches the red.
This snippet shows how to test the if two non-rotated rectangles are intersecting. The only real part of the code that does this is:
rectA.left< rectB.right&&
rectA.right> rectB.left&&
rectA.top< rectB.bottom&&
rectA.bottom> rectB.top
With an understanding of how x/y coordinates work on the web, it can be fun to draw this out and figure out why it works on your own.
I’ve used getBoundingClientRect to obtain the rectangles of two divs here. In the rare case where you need to calculate many many intersections frequently, getBoundingClientRect should be avoided if possible as it can get slow for a variety of reasons. The alternative to getBoundingClientRect is to keep track of all the rectangle coordinate and size information yourself.
This snippet takes input into a textfield and replaces all vowels with the letter “o”. I saw a meme that did something like this with musical instruments piano = poono, guitar = gootor etc..
document.addEventListener(hasTouch ?'touchstart':'mousedown', e =>{
// global id `mainSvg` :P
const{ locX, locY }= svgTouch(e, mainSvg);
const circle = createSvg('circle');
circle.cx.baseVal.value= locX;
circle.cy.baseVal.value= locY;
circle.r.baseVal.value=20;
circle.style.fill='blue';
mainSvg.appendChild(circle);
});
This snippet shows how to get relative mouse/touch coordinates within an SVG element. The main trick here is on line 50… pt.matrixTransform(svgEl.getScreenCTM().inverse()); – we get the inverse transformation matrix of the “mainSvg” node and transform the mouse/touch coordinates by it.
Beware, when this was posted, as noted in the comments – CSS transforms on the SVG element or any of its parents won’t be accounted for in the `getScreenCTM` call in any browsers other than Chrome. There is an open Firefox issue for this I believe…
Move your mouse on desktop or your finger on mobile – watch the red dot follow…
This is a simpler version of some of the things used in yesterdays post – just a quick way to normalize touch events – just one of many ways to do this – for simple stuff this is my goto.
If you want to try it out on its own page – take a look here (specifically good for trying it on mobile).
You’ll probably want a to use a meta tag like this – for the full effect.