Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var c=document.getElementById("myCanvas");
- var ctx=c.getContext("2d");
- ctx.rect(20,20,150,100);
- ctx.stroke();
- $('c.[rectangle]').hover(function(this){
- this.fillStyle = 'red';
- this.fill();
- });
- <!DOCTYPE html>
- <html>
- <body>
- <canvas id="myCanvas" width="700" height="500" style="border:1px solid #c3c3c3;">
- Your browser does not support the HTML5 canvas tag.
- </canvas>
- <script>
- var myRect={x:150, y:75, w:50, h:50, color:"red"};
- var c = document.getElementById("myCanvas");
- var ctx = c.getContext("2d");
- ctx.fillStyle = myRect.color;
- ctx.fillRect(myRect.x, myRect.y, myRect.w, myRect.h);
- c.addEventListener("mousemove", function(e){
- if ((e.clientX>=myRect.x)&(e.clientX<=myRect.x+myRect.w)&(e.clientY>=myRect.y)&(e.clientY<=myRect.y+myRect.h)){
- myRect.color = "green";}
- else{
- myRect.color = "red";}
- updateCanvas();
- }, false);
- function updateCanvas(){
- ctx.fillStyle = myRect.color;
- ctx.fillRect(myRect.x, myRect.y, myRect.w, myRect.h);
- }
- </script>
- </body>
- </html>
- var canvas = document.getElementById('canvas0');
- canvas.addEventListener('mouseover', function() { /*your code*/ }, false);
- var c=document.getElementById("myCanvas");
- var ctx=c.getContext("2d");
- ctx.rect(20,20,150,100);
- ctx.stroke();
- c.addEventListener("mouseover", doMouseOver, false);//added event to canvas
- function doMouseOver(e){
- ctx.fillStyle = 'red';
- ctx.fill();
- }
- const canvas = document.querySelector('canvas');
- const ctx = canvas.getContext('2d');
- const width = canvas.width = window.innerWidth;
- const height = canvas.height = window.innerHeight;
- const cx = width / 2;
- const cy = height / 2;
- const twoPie = Math.PI * 2;
- const points = []; // This will be the array we store our hover points in later
- class Point {
- constructor(x, y, r) {
- this.x = x;
- this.y = y;
- this.r = r || 0;
- }
- }
- class HoverPoint extends Point {
- constructor(x, y, r, color, hoverColor) {
- super(x, y, r);
- this.color = color;
- this.hoverColor = hoverColor;
- this.hovered = false;
- this.path = new Path2D();
- }
- draw() {
- this.hovered ? ctx.fillStyle = this.hoverColor : ctx.fillStyle = this.color;
- this.path.arc(this.x, this.y, this.r, 0, twoPie);
- ctx.fill(this.path);
- }
- }
- class Cursor extends Point {
- constructor(x, y, r) {
- super(x, y, r);
- }
- collisionCheck(points) {
- // This is the method that will be called during the animate function that
- // will check the cursors position against each of our objects in the points array.
- document.body.style.cursor = "default";
- points.forEach(point => {
- point.hovered = false;
- if (ctx.isPointInPath(point.path, this.x, this.y)) {
- document.body.style.cursor = "pointer";
- point.hovered = true;
- }
- });
- }
- }
- function createPoints() {
- // Create your points and add them to the points array.
- points.push(new HoverPoint(cx, cy, 100, 'red', 'coral'));
- points.push(new HoverPoint(cx + 250, cy - 100, 50, 'teal', 'skyBlue'));
- // ....
- }
- function update() {
- ctx.clearRect(0, 0, width, height);
- points.forEach(point => point.draw());
- }
- function animate(e) {
- const cursor = new Cursor(e.offsetX, e.offsetY);
- update();
- cursor.collisionCheck(points);
- }
- createPoints();
- update();
- canvas.onmousemove = animate;
- class Cursor extends Point {
- constructor(x, y, r) {
- super(x, y, r);
- }
- collisionCheck(points) {
- document.body.style.cursor = "default";
- points.forEach(point => {
- let dx = point.x - this.x;
- let dy = point.y - this.y;
- let distance = Math.hypot(dx, dy);
- let dr = point.r + this.r;
- point.hovered = false;
- // If the distance between the two objects is less then their combined radius
- // then they must be touching.
- if (distance < dr) {
- document.body.style.cursor = "pointer";
- point.hovered = true;
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement