canvas container page offset code example
Example: canvas container page offset
var canvas = document.querySelector('.canvas');
var originalWidth = canvas.width;
var originalHeight = canvas.height;
var context = canvas.getContext('2d');
var blockSize = originalWidth / 5;
context.fillStyle = '#6495ED';
for (var i = 0; i < originalWidth; i += blockSize) {
for (var j = 0; j < originalHeight; j += blockSize) {
context.fillRect(i, j, blockSize / 2, blockSize / 2);
}
}
function getBounds() {
return canvas.getBoundingClientRect();
}
function getOffset() {
var bounds = getBounds();
return {
x: bounds.left + window.scrollX,
y: bounds.top + window.scrollY,
};
}
var resizeButton = document.querySelector('.resize');
var isResized = false;
resizeButton.addEventListener('click', function() {
var newWidth = 50;
var newHeight = 50;
if (!isResized) {
canvas.setAttribute('style', 'width: ' + newWidth + 'px; height: ' + newHeight + 'px');
} else {
canvas.removeAttribute('style');
}
updateDataList();
isResized = !isResized;
});
var toggleButton = document.querySelector('.toggle');
var container = document.querySelector('.container');
var alignment = 'center';
function alignContainer() {
var label = '';
switch (alignment) {
case 'center':
container.setAttribute('class', 'container');
label = 'Centered';
alignment = 'right';
break;
case 'right':
container.setAttribute('class', 'container to-right');
label = 'Right Aligned';
alignment = 'bottom';
break;
case 'bottom':
container.setAttribute('class', 'container to-bottom');
label = 'Bottom Aligned';
alignment = 'left';
break;
case 'left':
container.setAttribute('class', 'container to-left');
label = 'Left Aligned';
alignment = 'top';
break;
case 'top':
container.setAttribute('class', 'container to-top');
label = 'Top Aligned';
alignment = 'center';
break;
}
toggleButton.innerHTML = label;
updateDataList();
}
toggleButton.addEventListener('click', function() {
alignContainer();
});
var list = document.querySelector('.data');
function updateDataList(data) {
var bounds = getBounds();
var offset = getOffset();
var currentWidth = bounds.width;
var currentHeight = bounds.height;
var scaleX = currentWidth / originalWidth;
var scaleY = currentHeight / originalHeight;
var value;
var mouse;
var mouseScaledX;
var mouseScaledY;
value = 'width: ' + originalWidth + 'px, height: ' + originalHeight + 'px';
list.querySelector('.original .value').innerHTML = value;
value = 'x: ' + scaleX + ', y: ' + scaleY;
list.querySelector('.scale .value').innerHTML = value;
value = 'x: ' + bounds.x + 'px, y: ' + bounds.y + 'px, ' +
'width: ' + bounds.width + 'px, height: ' + bounds.height + 'px';
list.querySelector('.bounds .value').innerHTML = value;
if (data && data.mouse) {
mouse = data.mouse;
mouseScaledX = mouse.unscaled.x * (1 / scaleX);
mouseScaledY = mouse.unscaled.y * (1 / scaleY);
value = 'unscaledX: ' + mouse.unscaled.x + 'px, unscaledY: ' + mouse.unscaled.y + 'px, ' +
'scaledX: ' + mouseScaledX + 'px, scaledY: ' + mouseScaledY + 'px';
list.querySelector('.mouse .value').innerHTML = value;
}
}
alignContainer();
canvas.addEventListener('mousemove', (e) => {
var mouseX = e.clientX - canvas.offsetLeft;
var mouseY = e.clientY - canvas.offsetTop;
updateDataList({
mouse: {
unscaled: {
x: mouseX,
y: mouseY
}
}
});
});