Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Handling Mouse and Keyboard Events | Text Images and Visual Styling
JavaScript Canvas Drawing and Animation

bookHandling Mouse and Keyboard Events

index.html

index.html

copy

When you want to make your canvas interactive, you need to capture user actions such as mouse clicks, movement, and even keyboard presses. Event listeners attached to the canvas element allow you to respond to these actions. Mouse events like click, mousedown, mouseup, and mousemove provide an event object with useful properties. The most common properties for canvas interaction include:

  • offsetX and offsetY: give the mouse position relative to the top-left corner of the canvas;
  • button: tells you which mouse button was pressed (0 for left, 1 for middle, 2 for right);
  • ctrlKey, shiftKey, altKey: indicate whether these modifier keys are pressed.

It is important to use offsetX and offsetY for accurate positioning on the canvas. Properties like clientX and pageX refer to positions relative to the browser window or the full page, not the canvas itself. Mapping the event's coordinates to the canvas space ensures your drawings or interactions appear exactly where the user expects. If your canvas is scaled or transformed, you may need to adjust these coordinates accordingly.

index.html

index.html

copy

You can also listen for keyboard events to control elements on your canvas. By responding to keydown or keyup events, you can move shapes, trigger actions, or change visual states. Combining mouse and keyboard input lets you create richer, more dynamic interactions. For instance, you might let users draw with the mouse while holding a key to switch colors, or move a character with the keyboard and interact with objects using the mouse. This combination forms the basis for interactive games, drawing tools, and creative visualizations.

1. Which event type is used to detect mouse movement over the canvas?

2. What property of a mouse event gives the position of the cursor relative to the canvas?

question mark

Which event type is used to detect mouse movement over the canvas?

Select the correct answer

question mark

What property of a mouse event gives the position of the cursor relative to the canvas?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 5

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you show me an example of how to add event listeners to a canvas?

How do I handle both mouse and keyboard events together?

What are some common use cases for combining mouse and keyboard input on a canvas?

Awesome!

Completion rate improved to 5.88

bookHandling Mouse and Keyboard Events

Свайпніть щоб показати меню

index.html

index.html

copy

When you want to make your canvas interactive, you need to capture user actions such as mouse clicks, movement, and even keyboard presses. Event listeners attached to the canvas element allow you to respond to these actions. Mouse events like click, mousedown, mouseup, and mousemove provide an event object with useful properties. The most common properties for canvas interaction include:

  • offsetX and offsetY: give the mouse position relative to the top-left corner of the canvas;
  • button: tells you which mouse button was pressed (0 for left, 1 for middle, 2 for right);
  • ctrlKey, shiftKey, altKey: indicate whether these modifier keys are pressed.

It is important to use offsetX and offsetY for accurate positioning on the canvas. Properties like clientX and pageX refer to positions relative to the browser window or the full page, not the canvas itself. Mapping the event's coordinates to the canvas space ensures your drawings or interactions appear exactly where the user expects. If your canvas is scaled or transformed, you may need to adjust these coordinates accordingly.

index.html

index.html

copy

You can also listen for keyboard events to control elements on your canvas. By responding to keydown or keyup events, you can move shapes, trigger actions, or change visual states. Combining mouse and keyboard input lets you create richer, more dynamic interactions. For instance, you might let users draw with the mouse while holding a key to switch colors, or move a character with the keyboard and interact with objects using the mouse. This combination forms the basis for interactive games, drawing tools, and creative visualizations.

1. Which event type is used to detect mouse movement over the canvas?

2. What property of a mouse event gives the position of the cursor relative to the canvas?

question mark

Which event type is used to detect mouse movement over the canvas?

Select the correct answer

question mark

What property of a mouse event gives the position of the cursor relative to the canvas?

Select the correct answer

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 5
some-alt