Oh shit, I fixed both errors literally minutes later
- When calling navigator.share, Files have to have a MIME type - Oh my fucking god I forgot to add will-change: transform - The whole image can now be seen at start. No more having to zoom out :D
This commit is contained in:
2 files changed
+32
-34
No files matched your search
+28
-32
@@ -118,8 +118,15 @@ Sakura.onload('editor', app => {
|
|||||||
const canvasBox = app.view.querySelector('.cbox')
|
const canvasBox = app.view.querySelector('.cbox')
|
||||||
canvasBox.appendChild(app.mirror)
|
canvasBox.appendChild(app.mirror)
|
||||||
const rect = canvasBox.getBoundingClientRect()
|
const rect = canvasBox.getBoundingClientRect()
|
||||||
const initialX = rect.width / 2 - app.image.naturalWidth / 2
|
|
||||||
const initialY = rect.height / 2 - app.image.naturalHeight / 2
|
// Scale so the whole image can be seen at the start
|
||||||
|
const initialScale = Math.min(
|
||||||
|
window.innerWidth / app.image.naturalWidth,
|
||||||
|
window.innerHeight / app.image.naturalHeight,
|
||||||
|
1
|
||||||
|
)
|
||||||
|
const initialX = (rect.width / 2) - (app.image.naturalWidth * initialScale / 2)
|
||||||
|
const initialY = (rect.height / 2) - (app.image.naturalHeight * initialScale / 2)
|
||||||
|
|
||||||
const canvas = app.mirror.transferControlToOffscreen()
|
const canvas = app.mirror.transferControlToOffscreen()
|
||||||
app.worker.postMessage({
|
app.worker.postMessage({
|
||||||
@@ -130,6 +137,7 @@ Sakura.onload('editor', app => {
|
|||||||
|
|
||||||
canvasBox.style.setProperty('--x', `${initialX}px`)
|
canvasBox.style.setProperty('--x', `${initialX}px`)
|
||||||
canvasBox.style.setProperty('--y', `${initialY}px`)
|
canvasBox.style.setProperty('--y', `${initialY}px`)
|
||||||
|
canvasBox.style.setProperty('--scale', `${initialScale}`)
|
||||||
|
|
||||||
touchManip({
|
touchManip({
|
||||||
canvasBox,
|
canvasBox,
|
||||||
@@ -163,15 +171,16 @@ Sakura.onload('editor', app => {
|
|||||||
|
|
||||||
shareBtn.addEventListener('click', () => {
|
shareBtn.addEventListener('click', () => {
|
||||||
Sakura.mirror.toBlob(blob => {
|
Sakura.mirror.toBlob(blob => {
|
||||||
const file = new File([blob], Sakura.image.title)
|
const file = new File([blob], Sakura.image.title, { type: blob.type })
|
||||||
console.log(file)
|
console.log(file)
|
||||||
navigator.share({
|
navigator.share({
|
||||||
// @ts-expect-error -- VS doesn't know about the new Files parameter
|
// @ts-expect-error -- VS doesn't know about the new Files parameter
|
||||||
files: [file],
|
files: [file],
|
||||||
text: '🌸'
|
text: '🌸'
|
||||||
}).catch(reason => {
|
}).catch(reason => {
|
||||||
console.log(reason)
|
if (("" + reason).endsWith('Share canceled')) return
|
||||||
alert("Dang, your device didn't let me do that.")
|
|
||||||
|
alert("Oops, your device didn't let me do that.")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -187,7 +196,7 @@ Sakura.onload('editor', app => {
|
|||||||
*/
|
*/
|
||||||
function touchManip({canvasBox, rect, image}) {
|
function touchManip({canvasBox, rect, image}) {
|
||||||
const SCROLL_SCALE = 480
|
const SCROLL_SCALE = 480
|
||||||
const PINCH_SCALE = 600
|
const PINCH_SCALE = 500
|
||||||
const PADDING_BOX = 64
|
const PADDING_BOX = 64
|
||||||
|
|
||||||
// Movement event listeners
|
// Movement event listeners
|
||||||
@@ -222,7 +231,7 @@ function touchManip({canvasBox, rect, image}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
canvasBox.addEventListener('wheel', ev => pointer.aggregateScroll -= ev.deltaY / SCROLL_SCALE, { passive: true })
|
canvasBox.addEventListener('wheel', ev => pointer.aggregateScroll -= ev.deltaY / SCROLL_SCALE, { passive: true })
|
||||||
canvasBox.addEventListener('mousemove', pointer.onPointerMove)
|
canvasBox.addEventListener('mousemove', pointer.onPointerMove, { passive: true })
|
||||||
canvasBox.addEventListener('touchmove', ev => {
|
canvasBox.addEventListener('touchmove', ev => {
|
||||||
const left = ev.targetTouches[0]
|
const left = ev.targetTouches[0]
|
||||||
const right = ev.targetTouches[1]
|
const right = ev.targetTouches[1]
|
||||||
@@ -252,7 +261,7 @@ function touchManip({canvasBox, rect, image}) {
|
|||||||
}
|
}
|
||||||
}, { passive: true })
|
}, { passive: true })
|
||||||
|
|
||||||
canvasBox.addEventListener('mousedown', pointer.onPointerDown)
|
canvasBox.addEventListener('mousedown', pointer.onPointerDown, { passive: true })
|
||||||
canvasBox.addEventListener('touchstart', ev => pointer.onPointerDown({
|
canvasBox.addEventListener('touchstart', ev => pointer.onPointerDown({
|
||||||
x: ev.targetTouches[0].clientX,
|
x: ev.targetTouches[0].clientX,
|
||||||
y: ev.targetTouches[0].clientY
|
y: ev.targetTouches[0].clientY
|
||||||
@@ -260,49 +269,36 @@ function touchManip({canvasBox, rect, image}) {
|
|||||||
canvasBox.addEventListener('touchend', () => {
|
canvasBox.addEventListener('touchend', () => {
|
||||||
pointer.isPanning = false
|
pointer.isPanning = false
|
||||||
pointer.multitouch = true
|
pointer.multitouch = true
|
||||||
})
|
}, { passive: true })
|
||||||
canvasBox.addEventListener('mouseup', () => pointer.isPanning = false)
|
canvasBox.addEventListener('mouseup', () => pointer.isPanning = false, { passive: true })
|
||||||
canvasBox.addEventListener('mouseleave', () => pointer.isPanning = false)
|
canvasBox.addEventListener('mouseleave', () => pointer.isPanning = false, { passive: true })
|
||||||
|
|
||||||
// IDK why this is janky
|
|
||||||
// I've run the profiler in Chrome several times, it looks like this callback's hitting 60fps
|
|
||||||
// but Chrome refuses to paint no more than about 5fps on my Pixel. Please uncomment the lines and try this for yourself
|
|
||||||
|
|
||||||
// let prev = performance.now()
|
|
||||||
function update() {
|
function update() {
|
||||||
// if (prev) console.log(1000 / (performance.now() - prev))
|
|
||||||
// prev = performance.now()
|
|
||||||
const computed = getComputedStyle(canvasBox)
|
const computed = getComputedStyle(canvasBox)
|
||||||
|
|
||||||
// Scroll
|
// Scroll
|
||||||
const parsedScale = +computed.getPropertyValue('--scale')
|
const prevScale = +computed.getPropertyValue('--scale')
|
||||||
// const parsedScale = +(canvasBox.dataset.scale || '')
|
// const parsedScale = +(canvasBox.dataset.scale || '')
|
||||||
// .1 <= scale <= 20
|
// .1 <= scale <= 20
|
||||||
const scale = Math.max(.1, Math.min(20, parsedScale + pointer.aggregateScroll))
|
const scale = Math.max(.1, Math.min(20, prevScale + pointer.aggregateScroll))
|
||||||
canvasBox.style.setProperty('--scale', '' + scale)
|
|
||||||
|
|
||||||
// Position
|
// Position
|
||||||
// const parsedX = +(canvasBox.dataset.x || '')
|
const prevX = +computed.getPropertyValue('--x').slice(0, -2)
|
||||||
// const parsedY = +(canvasBox.dataset.y || '')
|
const prevY = +computed.getPropertyValue('--y').slice(0, -2)
|
||||||
const parsedX = +computed.getPropertyValue('--x').slice(0, -2)
|
|
||||||
const parsedY = +computed.getPropertyValue('--y').slice(0, -2)
|
|
||||||
|
|
||||||
const x = Math.max(
|
const x = Math.max(
|
||||||
-image.width + PADDING_BOX,
|
-image.width + PADDING_BOX,
|
||||||
Math.min(rect.width - PADDING_BOX, parsedX + pointer.aggregateX)
|
Math.min(rect.width - PADDING_BOX, prevX + pointer.aggregateX)
|
||||||
)
|
)
|
||||||
const y = Math.max(
|
const y = Math.max(
|
||||||
-image.height + PADDING_BOX,
|
-image.height + PADDING_BOX,
|
||||||
Math.min(rect.height - PADDING_BOX, parsedY + pointer.aggregateY)
|
Math.min(rect.height - PADDING_BOX, prevY + pointer.aggregateY)
|
||||||
)
|
)
|
||||||
|
|
||||||
canvasBox.style.setProperty('--x', `${x}px`)
|
canvasBox.style.setProperty('--x', `${x}px`)
|
||||||
canvasBox.style.setProperty('--y', `${y}px`)
|
canvasBox.style.setProperty('--y', `${y}px`)
|
||||||
// canvasBox.setAttribute('style', `transform: translate(${x}px, ${y}px) scale(${scale})`)
|
canvasBox.style.setProperty('--scale', `${scale}`)
|
||||||
// canvasBox.dataset.x = '' + x
|
|
||||||
// canvasBox.dataset.y = '' + y
|
|
||||||
// canvasBox.dataset.scale = '' + scale
|
|
||||||
|
|
||||||
pointer.aggregateX = 0
|
pointer.aggregateX = 0
|
||||||
pointer.aggregateY = 0
|
pointer.aggregateY = 0
|
||||||
pointer.aggregateScroll = 0
|
pointer.aggregateScroll = 0
|
||||||
|
|||||||
@@ -111,14 +111,16 @@ button {
|
|||||||
}
|
}
|
||||||
.cbox {
|
.cbox {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
overflow: visible;
|
overflow: hidden;
|
||||||
--x: 0px;
|
--x: 0px;
|
||||||
--y: 0px;
|
--y: 0px;
|
||||||
--scale: 1;
|
--scale: 1;
|
||||||
|
touch-action: none;
|
||||||
}
|
}
|
||||||
.cbox > canvas {
|
.cbox > canvas {
|
||||||
transform: translate(var(--x), var(--y)) scale(var(--scale));
|
transform: translate(var(--x), var(--y)) scale(var(--scale));
|
||||||
/* background-color: var(--pink-hard); */
|
transform-origin: 0 0;
|
||||||
|
will-change: transform;
|
||||||
}
|
}
|
||||||
|
|
||||||
.drawer {
|
.drawer {
|
||||||
|
|||||||
Reference in new issue
Block a user