processing / processing/p5.js

Hit detection on 2D Shapes

Open
#7,081 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area:Events Area:Math Area:Utilities Feature Request
Dominant language
JavaScript
Stars
24k
Forks
3.8k
Avg merge
3d 16h
Merged PRs (30d)
25

Description

Increasing access

Useful for mouse interaction (mousehover, mouseclick, etc.) on 2DShapes (rect, ellipse, beginShape, ...)

Most appropriate sub-area of p5.js?
  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)
Feature request details

Using CanvasRenderingContext2D: isPointInPath() and isPointInStroke() this could be doable.

Here are two sketches showing how it would work,
the shape is drawn two times, the first one for hit detection (drawn as transparent), and the second for styling (fill, stroke, ...)

Example

const hit = hitTest(mouseX, mouseY, () => {
  rect(0, 0, 500, 500)
})

fill('black')
stroke('white')
cursor(ARROW)

if (hit.hasHit) cursor(HAND)

if (hit.inStroke) stroke('red')
else if (hit.inFill) fill('red')

hit.draw()

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked hit-detection sketches and the CanvasRenderingContext2D isPointInPath() and isPointInStroke() APIs. Compare the proposed hitTest() behavior for filled, stroked, and overlapping 2D shapes. Done should include a decided API and reliable detection of whether a point is in a shape's fill or stroke.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
computer-graphics
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.