Skip to content

Commit

Permalink
perf: Increase sampling speed
Browse files Browse the repository at this point in the history
  • Loading branch information
qq15725 committed May 12, 2023
1 parent 2a98604 commit 483ac66
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
31 changes: 15 additions & 16 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

const maxColors = 16 * 16
const size = Math.sqrt(maxColors)
const img = document.querySelector('img')
const el = document.querySelector('canvas')
const elContext2d = el.getContext('2d')
el.width = size
Expand All @@ -23,26 +24,24 @@
maxColors,
})

palette.addSample('/example.jpg').then(() => {
// eslint-disable-next-line no-console
console.time('generate')
palette.generate()
// eslint-disable-next-line no-console
console.timeEnd('generate')
// eslint-disable-next-line no-console
console.time('addSample')
palette.addSample(img)
// eslint-disable-next-line no-console
console.timeEnd('addSample')

elContext2d.putImageData(new ImageData(palette.getColors('buffer'), size, size), 0, 0)
// eslint-disable-next-line no-console
console.time('generate')
palette.generate()
// eslint-disable-next-line no-console
console.timeEnd('generate')

const colors = palette.getColors('hex')
elContext2d.putImageData(new ImageData(palette.getColors('buffer'), size, size), 0, 0)

// eslint-disable-next-line no-console
console.log(palette)
const colors = palette.getColors('hex')

// eslint-disable-next-line no-console
console.log(colors)

// eslint-disable-next-line no-console
console.log(palette.findNearestColor('#ffffff'))
})
// eslint-disable-next-line no-console
console.log(palette, colors, palette.findNearestColor('#ffffff'))
</script>
</body>
</html>
6 changes: 3 additions & 3 deletions src/add-sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function addSample(
context2d.clearRect(0, 0, canvas.width, canvas.height)
canvas.width = img.width
canvas.height = img.height
context2d.drawImage(img, 0, 0)
context2d.drawImage(img, 0, 0, canvas.width, canvas.height)
return addSample(
context,
context2d.getImageData(0, 0, img.width, img.height).data,
Expand All @@ -64,7 +64,7 @@ export function addSample(
context2d.clearRect(0, 0, canvas.width, canvas.height)
canvas.width = typeof source.width === 'number' ? source.width : source.width.baseVal.value
canvas.height = typeof source.height === 'number' ? source.height : source.height.baseVal.value
context2d.drawImage(source, 0, 0)
context2d.drawImage(source, 0, 0, canvas.width, canvas.height)
return addSample(
context,
context2d.getImageData(0, 0, canvas.width, canvas.height).data,
Expand All @@ -91,7 +91,7 @@ export function addSample(
}

const srgb = (r << 16) | (g << 8) | b
const key = Number(`${ a }${ srgb }`)
const key = a * 100000000 + srgb

let index = colorSamplesCache.get(key)
if (index !== undefined) {
Expand Down

1 comment on commit 483ac66

@vercel
Copy link

@vercel vercel bot commented on 483ac66 May 12, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

modern-palette – ./

modern-palette.vercel.app
modern-palette-git-main-qq15725.vercel.app
modern-palette-qq15725.vercel.app

Please sign in to comment.