Identifying the pointermove event occurring outside the div when the pointer is held down

Recently, I put my skills to the test with an interesting challenge. My main objective was to replace all HTML tags with div elements.

Objective: Develop a color picker similar to the one found in Photoshop

Requirements: Utilize HTML5, JavaScript, and CSS only (with ReactJS for structural development)

Accomplishments so far:

  • Used onPointerMove and onPointerDown events to track pointer position in the viewport
  • Successfully calculated and positioned the indicator based on pointer location
  • Obtained selected color in RGB format

Current HTML structure:

let padding = props.size * 0.1;
<div className="color-picker">
    <div className='color-picker-container' style={ { width: `${props.size + 2 * padding}px` } } onPointerMove={ onMove } onPointerDown={ onMove }>
        <div className="color-picker-panel" style={ { width: `${props.size}px` } }/>
        <div className="color-picker-indicator" style={ { left: pickerPos.left, top: pickerPos.top, borderColor: `rgb(${getInvertedColorChannel(ColorChannel.R, currentMaxColor)}, ${getInvertedColorChannel(ColorChannel.G, currentMaxColor)}, ${getInvertedColorChannel(ColorChannel.B, currentMaxColor)}` } }/>
    </div>
</div>

Outcome:

https://i.sstatic.net/FwIai.png

Explanation of result:

  • The background color changes according to the selected color
  • The white area represents the padding of the container
  • Used pointer event within the container as a workaround for adding padding (as mentioned in "Problem")
  • The indicator can only be moved by dragging within the white area (including the color picker panel)

Challenge faced:

  • Pointer events are triggered only when directly interacting with the div element, preventing movement of the indicator outside the div (white area) even when the pointer is down.
  • The current solution involves adding padding to the color picker, but this method still limits interaction boundaries. The goal is to allow indicator movement after pointerdown, regardless of pointer position inside or outside the div.

Feel free to inquire for more details. Thank you!

Answer №1

After capturing the mouse down event within the div, add a mouseMove listener to <body> and monitor it from there. Once the mouse is released, remember to remove the listener.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the Depths of Dom-Repeat in Polymer 1.0: Harnessing the Power of Infinite

Here is the JSON object I am working with: categoryList = [{ "title": "Computers", "categories": [ { "title": "Laptops", "categories": [ { "title": "Ultrabooks", "categories ...

Sort and filter columns in jQuery datatable

My testing involved setting up a small grid to experiment with the JQuery DataTable column filter. However, upon running the site, I noticed that both the grid header and the row containing the input fields had the sorting class attribute. The versions of ...

Retrieve information from the database without the need to refresh the page

I recently implemented an Ajax function to update information in my database, but I encountered a problem. The element displaying this updated information does not reflect the changes immediately; only after refreshing the page and reconnecting to the data ...

Dynamic Menu Utilizes jQuery to Load Content from External File

My objective here is: Title0 --Subtitle1 - Hidden - onClick toggle() --Subtitle2 - Hidden - onClick toggle() When "Title0" is clicked, Subtitle1 and Subtitle2 will appear on the left side of the page while content from another file loads. Once the other ...

Struggling with deploying a Node.js application on Heroku

After successfully testing and completing a version of my app that utilizes Node.js, specifically incorporating the node twitter module, I followed Heroku's deployment guide. When I ran heroku git:clone -a groupmetweetbot, the console output showed th ...

Creating a universally accessible handlebars helper in ExpressJS

I have a basic handlebars helper file located in helpers/handlebars.js: var hbs = require('express-handlebars'); hbs.registerHelper("inc", function(value, options) { return parseInt(value) + 1; }); Unfortunately, I am unable to utilize the ...

successive ajax requests

I am facing a challenge where I need to execute two separate ajax calls sequentially. The second call relies on the result of the first call for its data. Despite my efforts, I haven't been able to achieve the desired outcome. Here's what I have ...

Customize Popover Color in SelectField Component

Looking to customize the SelectField's popover background color in material-ui. Is this possible? After exploring the generated theme, it seems that there is no option for configuring the selectField or popover. Attempted adjusting the menu's ba ...

Having difficulty extracting information from an HTML page using Swift

I'm attempting to convert data retrieved from an HTML page into a readable format, but the string urlContent is showing as nil even though the data received from the NSURLSession is not null. This is my approach: var city = "London" var url = NSURL ...

Implementing external services in AngularJS with RequireJS: a step-by-step guide

I'm currently integrating a controller from an external file, and I would like to achieve the same for a service from an external file as well. The service should be registered in the factory statement. The injection of the controller is functioning ...

Adjust the transparency and add animation effects using React JS

When working on a React project, I encountered an issue where a square would appear under a paragraph when hovered over and disappear when no longer hovered. However, the transition was too abrupt for my liking, so I decided to implement a smoother change ...

Spin and shift image of a ball using Html 5

Now the image will be moved without rotating. I have implemented moving functionalities, but it only supports IE10. Here is the script I am using: var ball = new Image; window.onload = function () { var c = document.getElementsByTagName('canvas&apos ...

Obtain the identification number and full name from the typeahead feature

I am able to retrieve the name and ID, but I am only able to display the name. I have attempted using update and afterslected methods. Currently, I have this code which works well, however, it only fetches the name! $('input.typeahead').typea ...

javascript: Make sure to update the DOM only after the result has been successfully obtained

I have access to an API endpoint that returns server details (). There are also server-specific endpoints, for example () where I have to call each one for every server ID obtained from the "serverDetails" endpoint. My current approach involves looping ov ...

Managing multiple photo uploads using React JS and Laravel

I am trying to use axios in ReactJS to upload multiple images to the database, send the data from client-side to server-side, and handle image uploads with Laravel on the backend. However, I am encountering an issue when attempting to process multiple imag ...

Struggles with handling asynchronous events in Angular

I'm currently working on a piece of code that iterates through an array containing 10 items. For each item, a request is made and the returned data is stored in another array. The entire process goes smoothly until reaching the line that contains $q.a ...

Hover and Click Card Turning

My card can both hover and click, but there seems to be a small glitch. After clicking on the card and then moving the cursor away from the front, the hover effect doesn't work correctly. It immediately flips back to the front side. The hover effect ...

Use ASP.NET MVC to pass data from an action to an AJAX call and utilize it in the success function

I have a custom ResultOfOperation class that I use to retrieve details about an activity: public class ResultOfOperation { public string Message1 { get; set; } public string Message2 { get; set; } public string Message3 { get; ...

How can the center element be aligned without recursively centering all children?

How can I center all children within a particular element without affecting their children? <Col style={{ textAlign: "center" }}> <Space>...</Space> <div>...</div> </Col> In this scenario, I am utilizing t ...

Add Your Logo to the Navigation Bar using CSS

Can someone please assist me with adding a logo to my CSS navbar? I am struggling to figure out how to do this and would appreciate any guidance or tips on how to successfully implement a logo in the navbar. body { margin: 0px; } ul { list-style-ty ...