Skip to content
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

adds a button to toggle direct touch drawing #6

Open
wants to merge 3 commits into
base: gh-pages
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
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ <h1>Demo of Apple Pencil / 3D touch API</h1>
<div id="info">
<a href="https://github.com/quietshu/apple-pencil-safari-api-test" target="_blank">GitHub</a>
<button onclick="undoDraw()">Undo</button>
<button onclick="toggleDirect()">Toggle Direct</button>
</div>
<script src="index.js"></script>
</body>
</html>
</html>
84 changes: 52 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const context = canvas.getContext('2d')
let lineWidth = 0
let isMousedown = false
let points = []
let allowDirect = true;

canvas.width = window.innerWidth * 2
canvas.height = window.innerHeight * 2
Expand Down Expand Up @@ -63,29 +64,43 @@ function undoDraw () {
})
}

/**
* Disable or enable direct touch
* @return {void}
*/
function toggleDirect () {
$touches.innerHTML = $touches.innerHTML.replace(`allowDirect = ${String(allowDirect)}`, `allowDirect = ${String(!allowDirect)}`)
allowDirect = !allowDirect;
}

for (const ev of ["touchstart", "mousedown"]) {
canvas.addEventListener(ev, function (e) {
let pressure = 0.1;
let x, y;
if (e.touches && e.touches[0] && typeof e.touches[0]["force"] !== "undefined") {
if (e.touches[0]["force"] > 0) {
pressure = e.touches[0]["force"]

const touch = e.touches ? e.touches[0] : null
if(allowDirect || touch && touch.touchType != "direct"){

if (e.touches && e.touches[0] && typeof e.touches[0]["force"] !== "undefined") {
if (e.touches[0]["force"] > 0) {
pressure = e.touches[0]["force"]
}
x = e.touches[0].pageX * 2
y = e.touches[0].pageY * 2
} else {
pressure = 1.0
x = e.pageX * 2
y = e.pageY * 2
}
x = e.touches[0].pageX * 2
y = e.touches[0].pageY * 2
} else {
pressure = 1.0
x = e.pageX * 2
y = e.pageY * 2
}

isMousedown = true
isMousedown = true

lineWidth = Math.log(pressure + 1) * 40
context.lineWidth = lineWidth// pressure * 50;
lineWidth = Math.log(pressure + 1) * 40
context.lineWidth = lineWidth// pressure * 50;

points.push({ x, y, lineWidth })
drawOnCanvas(points)
points.push({ x, y, lineWidth })
drawOnCanvas(points)
}
})
}

Expand All @@ -94,30 +109,34 @@ for (const ev of ['touchmove', 'mousemove']) {
if (!isMousedown) return
e.preventDefault()

let pressure = 0.1
let x, y
if (e.touches && e.touches[0] && typeof e.touches[0]["force"] !== "undefined") {
if (e.touches[0]["force"] > 0) {
pressure = e.touches[0]["force"]
let pressure = 0.1;
let x, y;
const touch = e.touches ? e.touches[0] : null

if(allowDirect || touch && touch.touchType != "direct"){
let x, y
if (e.touches && e.touches[0] && typeof e.touches[0]["force"] !== "undefined") {
if (e.touches[0]["force"] > 0) {
pressure = e.touches[0]["force"]
}
x = e.touches[0].pageX * 2
y = e.touches[0].pageY * 2
} else {
pressure = 1.0
x = e.pageX * 2
y = e.pageY * 2
}
x = e.touches[0].pageX * 2
y = e.touches[0].pageY * 2
} else {
pressure = 1.0
x = e.pageX * 2
y = e.pageY * 2
}

// smoothen line width
lineWidth = (Math.log(pressure + 1) * 40 * 0.2 + lineWidth * 0.8)
points.push({ x, y, lineWidth })
// smoothen line width
lineWidth = (Math.log(pressure + 1) * 40 * 0.2 + lineWidth * 0.8)
points.push({ x, y, lineWidth })

drawOnCanvas(points);
drawOnCanvas(points);
}

requestIdleCallback(() => {
$force.textContent = 'force = ' + pressure

const touch = e.touches ? e.touches[0] : null
if (touch) {
$touches.innerHTML = `
touchType = ${touch.touchType} ${touch.touchType === 'direct' ? '👆' : '✍️'} <br/>
Expand All @@ -126,6 +145,7 @@ for (const ev of ['touchmove', 'mousemove']) {
rotationAngle = ${touch.rotationAngle} <br/>
altitudeAngle = ${touch.altitudeAngle} <br/>
azimuthAngle = ${touch.azimuthAngle} <br/>
allowDirect = ${allowDirect} <br/>
`
}
})
Expand Down