Skip to content

Pointer events tutorial #732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<script type="module" src="my-element.js"></script>
<style>
body {
font-family: 'Open Sans', sans-serif;
font-size: 1.5em;
padding-left: 0.5em;
}
</style>
</head>
<body>
<my-element></my-element>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {LitElement, html, css} from 'lit';
import {customElement, state} from 'lit/decorators.js';

@customElement('my-element')
class MyElement extends LitElement {
@state() log = '';

static styles = [css`
div {
height: 100px;
width: 100px;
background: grey;
touch-action: none;
text-align: middle;
vertical-align: middle;
}
`];

render() {
return html`
<div id="block"
@pointerdown=${this.handleEvent}
@pointerup=${this.handleEvent}
@pointerenter=${this.handleEvent}
@pointerleave=${this.handleEvent}
>Touch Here</div>
<pre>${this.log}</pre>
`;
}

handleEvent(ev: PointerEvent) {
this.log += `${ev.type} from pointer ${ev.pointerId}\n`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "/samples/base.json",
"files": {
"my-element.ts": {},
"index.html": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<script type="module" src="my-element.js"></script>
<style>
body {
font-family: 'Open Sans', sans-serif;
font-size: 1.5em;
padding-left: 0.5em;
}
</style>
</head>
<body>
<my-element></my-element>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {LitElement, html, css} from 'lit';
import {customElement, state} from 'lit/decorators.js';

@customElement('my-element')
class MyElement extends LitElement {
@state() log = '';

static styles = [css`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest some playground folds here to hide things like styling and focus the reader's attention on the parts that matter

div {
height: 100px;
width: 100px;
background: grey;
touch-action: none;
text-align: middle;
vertical-align: middle;
}
`];

render() {
return html`
<div id="block"
@pointerdown=${this.handleEvent}
@pointerup=${this.handleEvent}
@pointermove=${this.handleEvent}
@pointerenter=${this.handleEvent}
@pointerleave=${this.handleEvent}
>Touch Here</div>
<pre>${this.log}</pre>
`;
}

handleEvent(ev: PointerEvent) {
this.log += `${ev.type} from pointer ${ev.pointerId}\n`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "/samples/base.json",
"files": {
"my-element.ts": {},
"index.html": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<script type="module" src="my-element.js"></script>
<style>
body {
font-family: 'Open Sans', sans-serif;
font-size: 1.5em;
padding-left: 0.5em;
}
</style>
</head>
<body>
<my-element></my-element>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {LitElement, html, css} from 'lit';
import {customElement, state} from 'lit/decorators.js';

@customElement('my-element')
class MyElement extends LitElement {
@state() log = '';

static styles = [css`
div {
height: 100px;
width: 100px;
background: grey;
touch-action: none;
text-align: middle;
vertical-align: middle;
}
`];

render() {
return html`
<div id="block"
@pointerdown=${this.handleEvent}
>Touch Here</div>
<pre>${this.log}</pre>
`;
}

handleEvent(ev: PointerEvent) {
const props = new Set([
'x', 'y', 'pointerId', 'pointerType', 'isPrimary', 'pressure', 'tiltX', 'tiltY', 'width', 'height', 'twist', 'buttons'
]);
const copy: {[key: string]: unknown} = {};
for (let key in ev) {
if (props.has(key)) {
copy[key] = (ev as any)[key];
}
}
this.log = JSON.stringify(copy, null, 2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "/samples/base.json",
"files": {
"my-element.ts": {},
"index.html": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<script type="module" src="my-element.js"></script>
<style>
body {
font-family: 'Open Sans', sans-serif;
font-size: 1.5em;
padding-left: 0.5em;
}
</style>
</head>
<body>
<my-element></my-element>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import {LitElement, html, css} from 'lit';
import {customElement, state, query} from 'lit/decorators.js';

@customElement('my-element')
class MyElement extends LitElement {
@state() log = '';
@state() enableMouseCapturing = false;
@state() disableTouchCapturing = false;
@query('#mousecapture') mouseCheckbox!: HTMLInputElement;
@query('#touchcapture') touchCheckbox!: HTMLInputElement;
@query('#block') target!: HTMLElement;

static styles = [css`
div {
height: 100px;
width: 100px;
background: grey;
user-select: none;
touch-action: none;
}
label {
font-size: 1rem;
}
`];

render() {
return html`
<label>Capture Mouse Pointer <input type="checkbox" id="mousecapture" @change=${this.setCapturing}></label>
<br>
<label>Release Touch Pointer <input type="checkbox" id="touchcapture" @change=${this.setTouchCapturing}></label>
<div
id="block"
@pointerdown=${this.handlePointer}
@pointermove=${this.handlePointer}
@pointerup=${this.handlePointer}
@pointercancel=${this.handlePointer}
>Touch Here</div>
<pre>${this.log}</pre>
`;
}

setCapturing() {
this.enableMouseCapturing = this.mouseCheckbox.checked;
}

setTouchCapturing() {
this.disableTouchCapturing = this.touchCheckbox.checked;
}

handlePointer(ev: PointerEvent) {
const {type: eventType, x, y, buttons, isPrimary, pointerId, pointerType} = ev;
// ignore mousemoves without holding a button and multiple fingers
if (!isPrimary || eventType === 'pointermove' && buttons === 0) {
return;
}
if (this.enableMouseCapturing && eventType === 'pointerdown' && pointerType !== 'touch') {
this.target.setPointerCapture(pointerId);
}
if (this.disableTouchCapturing && eventType === 'pointerdown' && pointerType === 'touch' ) {
this.target.releasePointerCapture(pointerId);
}
this.log = `${eventType} (${Math.round(x)}px, ${Math.round(y)}px)`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "/samples/base.json",
"files": {
"my-element.ts": {},
"index.html": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<script type="module" src="my-element.js"></script>
<style>
body {
font-family: 'Open Sans', sans-serif;
font-size: 1.5em;
padding-left: 0.5em;
}
</style>
</head>
<body>
<my-element></my-element>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {LitElement, html, css} from 'lit';
import {customElement, state, query} from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
import { repeat } from 'lit/directives/repeat.js';

const TOUCH_ACTIONS = [
'auto',
'none',
'pan-left',
'pan-right',
'pan-x',
'pan-up',
'pan-down',
'pan-y',
'pinch-zoom',
'manipulation'
];

@customElement('my-element')
class MyElement extends LitElement {
@query('select') select!: HTMLSelectElement;
@state() touchAction = 'auto';
@state() log = '';

static styles = [css`
div {
height: 150vh;
width: 150vw;
background: linear-gradient(to bottom right, #91ffff, #324fff);
user-select: none;
}
span {
font-size: 1rem;
}
pre {
position: fixed;
}
`];

render() {
return html`
<span>Touch action setting</span>
<select @change=${this.setTouchAction}>
${repeat(TOUCH_ACTIONS, (action) => html`<option>${action}</option>`)}
</select>
<br>
<br>
<div
id="block"
style=${styleMap({'touch-action': this.touchAction})}
@pointermove=${this.handlePointer}
@pointercancel=${this.handlePointer}
><pre>${this.log}</pre></div>
`;
}

setTouchAction() {
this.touchAction = this.select.value;
}

handlePointer(ev: PointerEvent) {
const {type, x, y, buttons} = ev;
if (type === 'pointermove' && buttons === 0) {
return;
}
this.log = `${type} (${x}px, ${y}px)`;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "/samples/base.json",
"files": {
"my-element.ts": {},
"index.html": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Learn how to handle PointerEvents with Lit.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"header": "Pointer Events",
"difficulty": "Beginner",
"size": "small",
"duration": 10,
"category": "Learn",
"steps": [
{
"title": "Intro to Pointer Events"
},
{
"title": "Event Types"
},
{
"title": "Pointer Event Properties"
},
{
"title": "Pointer Capturing"
},
{
"title": "Controlling scrolling with touch-action"
}
]
}
Loading