Write me a code to add to a Google website I made that will make a ball fallow the mouse…
- User
Write me a code to add to a Google website I made that will make a ball fallow the mouse pointer
- Assistant
To make a ball follow the mouse pointer in a Google website, you can use JavaScript and HTML. Here's an example code:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Ball Following Mouse</title> <style> #ball { width: 50px; height: 50px; border-radius: 50%; background-color: red; position: absolute; } </style> </head> <body> <div id="ball"></div> <script> var ball = document.getElementById("ball"); document.addEventListener("mousemove", function(event) { var x = event.clientX; var y = event.clientY; ball.style.left = (x - 25) + "px"; ball.style.top = (y - 25) + "px"; }); </script> </body> </html>This code creates a red ball with a diameter of 50 pixels and sets its position to absolute. Then, it adds an event listener to the document that listens for mouse movements. When the mouse moves, the event listener gets the X and Y coordinates of the mouse pointer and sets the position of the ball to those coordinates minus half of the ball's diameter (to center the ball on the pointer).
- User
Can you do a functional rewrite of this in React JSX?
- Assistant
import React, { useState } from "react";
function BallFollowingMouse() { const [position, setPosition] = useState({ x: 0, y: 0 });
function handleMouseMove(event) { setPosition({ x: event.clientX, y: event.clientY }); }
return ( <div style={{ position: "relative", width: "100%", height: "100%", overflow: "hidden", }} onMouseMove={handleMouseMove} > <div style={{ width: "50px", height: "50px", borderRadius: "50%", backgroundColor: "red", position: "absolute", top: position.y - 25, left: position.x - 25, }} /> ); }
export default BallFollowingMouse;
In this code, we define a functional component BallFollowingMouse that uses the React useState hook to manage the position of the ball. We create a handleMouseMove function that updates the state with the current mouse position whenever the mouse moves.
In the component's return statement, we render a container div with position: relative and overflow: hidden styles to constrain the ball's movement to the container. Inside this container, we render a div element for the ball itself. We set the ball's position using the state values and the same calculation as in the original code: top: position.y - 25 and left: position.x - 25.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →