canvas container page offset
JavaScript
// get the canvas and store its original dimensions
var canvas = document.querySelector('.canvas');
var originalWidth = canvas.width; // px
var originalHeight = canvas.height; // px
// draw something on the canvas
var context = canvas.getContext('2d');
var blockSize = originalWidth / 5; // px
context.fillStyle = '#6495ED'; // nice cornflowerblue
for (var i = 0; i < originalWidth; i += blockSize) {
for (var j = 0; j < originalHeight; j += blockSize) {
context.fillRect(i, j, blockSize / 2, blockSize / 2);
}
}
// get the bounds of the canvas element
function getBounds() {
return canvas.getBoundingClientRect();
}
// get the offset of the canvas relative to the document
function getOffset() {
var bounds = getBounds();
return {
x: bounds.left + window.scrollX,
y: bounds.top + window.scrollY,
};
}
// init the buttons for demonstration purposes
var resizeButton = document.querySelector('.resize');
var isResized = false;
resizeButton.addEventListener('click', function() {
var newWidth = 50; // px
var newHeight = 50; // px
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'; // centered by default
// align the container element between left, center, and right
function alignContainer() {
var label = '';
switch (alignment) {
// align to center
case 'center':
container.setAttribute('class', 'container');
label = 'Centered';
// next alingment;
alignment = 'right';
break;
// align to right
case 'right':
container.setAttribute('class', 'container to-right');
label = 'Right Aligned';
// next alingment;
alignment = 'bottom';
break;
// align to bottom
case 'bottom':
container.setAttribute('class', 'container to-bottom');
label = 'Bottom Aligned';
// next alingment;
alignment = 'left';
break;
// align to left
case 'left':
container.setAttribute('class', 'container to-left');
label = 'Left Aligned';
// next alingment;
alignment = 'top';
break;
// align to top
case 'top':
container.setAttribute('class', 'container to-top');
label = 'Top Aligned';
// next alingment;
alignment = 'center';
break;
}
toggleButton.innerHTML = label;
updateDataList();
}
toggleButton.addEventListener('click', function() {
alignContainer();
});
// update the data list according to the given data
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;
// update original dimensions
value = 'width: ' + originalWidth + 'px, height: ' + originalHeight + 'px';
list.querySelector('.original .value').innerHTML = value;
// update scale
value = 'x: ' + scaleX + ', y: ' + scaleY;
list.querySelector('.scale .value').innerHTML = value;
// update bounds
value = 'x: ' + bounds.x + 'px, y: ' + bounds.y + 'px, ' +
'width: ' + bounds.width + 'px, height: ' + bounds.height + 'px';
list.querySelector('.bounds .value').innerHTML = value;
// update mouse
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;
}
}
// initially align container
alignContainer();
// update data according to the event
canvas.addEventListener('mousemove', (e) => {
var mouseX = e.clientX - canvas.offsetLeft;
var mouseY = e.clientY - canvas.offsetTop;
updateDataList({
mouse: {
unscaled: {
x: mouseX,
y: mouseY
}
}
});
});
Also in JavaScript:
- Title
- javascript largest number in array
- Category
- JavaScript
- Title
- how to create react app
- Category
- JavaScript
- Title
- copy object javascript
- Category
- JavaScript
- Title
- endpoint js
- Category
- JavaScript
- Title
- create react app in existing folder
- Category
- JavaScript
- Title
- jacvascript parse float
- Category
- JavaScript
- Title
- angular run behind proxy
- Category
- JavaScript
- Title
- How to scan a folder for documents with javascript
- Category
- JavaScript
- Title
- How to insert an item into an array at a specific index JavaScript
- Category
- JavaScript
- Title
- javascript change element id
- Category
- JavaScript
- Title
- chart.js line chart animation on canvas update react class state
- Category
- JavaScript
- Title
- can you do a join() in js without the commas
- Category
- JavaScript
- Title
- how to make a javascript game
- Category
- JavaScript
- Title
- equal to or more than javascript
- Category
- JavaScript
- Title
- delete session javascript
- Category
- JavaScript
- Title
- javascript basic math
- Category
- JavaScript
- Title
- insertion sort js
- Category
- JavaScript
- Title
- angular wait all subscriptions
- Category
- JavaScript
- Title
- discord.js bot github
- Category
- JavaScript
- Title
- create child element in javascript
- Category
- JavaScript
- Title
- javascript check if browser is ie
- Category
- JavaScript
- Title
- foreach javascript
- Category
- JavaScript
- Title
- adjust() js
- Category
- JavaScript
- Title
- javascript check if element is visible on screen
- Category
- JavaScript
- Title
- array definition js
- Category
- JavaScript
- Title
- if statemnt shorthand js without else
- Category
- JavaScript
- Title
- how do i check if JQuery checkbox is checked
- Category
- JavaScript
- Title
- how to compare two strings in javascript if condition
- Category
- JavaScript
- Title
- angular event scroll end of page
- Category
- JavaScript
- Title
- call javascript function after page load complete
- Category
- JavaScript
- Title
- Factorial Number
- Category
- JavaScript
- Title
- fromjson method convert integer to double
- Category
- JavaScript
- Title
- injected stylesheet remove
- Category
- JavaScript
- Title
- javascript base 10 to base 2
- Category
- JavaScript
- Title
- how to build a string javascript es6
- Category
- JavaScript
- Title
- how to remove element from array in javascript
- Category
- JavaScript
- Title
- define a route with path paramters in nodejs
- Category
- JavaScript
- Title
- import reactdom
- Category
- JavaScript
- Title
- angularjs iframe src binding
- Category
- JavaScript
- Title
- how to position View absolute react native
- Category
- JavaScript
- Title
- cannot read property 'props' of undefined react redux functional component
- Category
- JavaScript
- Title
- export to logs to elasticsearch node from fluend container k8s
- Category
- JavaScript
- Title
- check node version
- Category
- JavaScript
- Title
- how to make a dictionary javascript
- Category
- JavaScript
- Title
- how to pass a value to a react funtion without immediately firing it
- Category
- JavaScript
- Title
- all ajaxcomplete event
- Category
- JavaScript
- Title
- install nuxt js
- Category
- JavaScript
- Title
- $pull mongoose
- Category
- JavaScript
- Title
- how to update angular version
- Category
- JavaScript
- Title
- html table to excel javascript
- Category
- JavaScript
- Title
- class component react
- Category
- JavaScript
- Title
- export function javascript
- Category
- JavaScript
- Title
- changing attributes using javascript
- Category
- JavaScript
- Title
- javascript add minutes to date
- Category
- JavaScript
- Title
- banking program deposit and withdrawal using ajax call
- Category
- JavaScript
- Title
- creating a 2d array in js
- Category
- JavaScript
- Title
- insert into specific array index
- Category
- JavaScript
- Title
- array of objects javascript
- Category
- JavaScript
- Title
- how to iterate array in javascript
- Category
- JavaScript
- Title
- disregard empty field in array
- Category
- JavaScript
- Title
- go to another page javascript
- Category
- JavaScript
- Title
- duplicate encoder
- Category
- JavaScript
- Title
- FUNCION EN OBJETO JAVASCRIPT
- Category
- JavaScript
- Title
- get value javascript
- Category
- JavaScript
- Title
- get and set es6
- Category
- JavaScript
- Title
- javascript append to array
- Category
- JavaScript
- Title
- get query parameters in node.js
- Category
- JavaScript
- Title
- clear cookies js
- Category
- JavaScript
- Title
- embed jquery
- Category
- JavaScript
- Title
- convert jquery to string
- Category
- JavaScript
- Title
- array chunk javascript
- Category
- JavaScript
- Title
- convert json to 2d array
- Category
- JavaScript
- Title
- Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
- Category
- JavaScript
- Title
- floor in js
- Category
- JavaScript
- Title
- bootstrap 4 form validator with jquery
- Category
- JavaScript
- Title
- get contents between tags javascript
- Category
- JavaScript
- Title
- get client timezone offset in JavaScript
- Category
- JavaScript
- Title
- Javascript compare two dates
- Category
- JavaScript
- Title
- how to call action from another module vuex
- Category
- JavaScript
- Title
- express and node
- Category
- JavaScript
- Title
- ajax with progress bar
- Category
- JavaScript
- Title
- change text of element js
- Category
- JavaScript
- Title
- how to use js console log
- Category
- JavaScript
- Title
- cb=gapi.loaded_0:190 Uncaught TypeError: $.ajax is not a function
- Category
- JavaScript
- Title
- angular $http abort request
- Category
- JavaScript
- Title
- adonisjs livereload
- Category
- JavaScript
- Title
- javascript checkbox value
- Category
- JavaScript
- Title
- create element javascript with class
- Category
- JavaScript
- Title
- how will it look when there is a container inside a a row bootstrap
- Category
- JavaScript
- Title
- fetch post js
- Category
- JavaScript
- Title
- example of while loop in javascript with array length
- Category
- JavaScript
- Title
- enter event in jquery
- Category
- JavaScript
- Title
- how to calculate balance automatically using javascript and php using cash paid
- Category
- JavaScript
- Title
- how to send enter event to input field jquery
- Category
- JavaScript
- Title
- how to open new tab in vscode using keyboard
- Category
- JavaScript
- Title
- age calculator javascript
- Category
- JavaScript
- Title
- devexpress custom column field
- Category
- JavaScript
- Title
- javascript base64 encode file input
- Category
- JavaScript
- Title
- git remote url change
- Category
- JavaScript
- Title
- get args nodejs
- Category
- JavaScript