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

A version that works in modern browsers (and typescript version) #25

Open
elliots opened this issue May 26, 2023 · 1 comment
Open

A version that works in modern browsers (and typescript version) #25

elliots opened this issue May 26, 2023 · 1 comment

Comments

@elliots
Copy link

elliots commented May 26, 2023

javascript:

/**
 * Display an image in the console.
 */
console.image = function (url, backgroundColour, scale) {
  const img = new Image()
  img.crossOrigin = "anonymous"
  img.onload = () => {
    const c = document.createElement('canvas')
    const ctx = c.getContext('2d')
    if (ctx) {
      c.width = img.width
      c.height = img.height
      ctx.fillStyle = backgroundColour;
      ctx.fillRect(0, 0, c.width, c.height);
      ctx.drawImage(img, 0, 0)
      const dataUri = c.toDataURL('image/png')

      console.log(`%c sup?` ,
        `
          font-size: 1px;
          padding: ${Math.floor((img.height * scale) / 2)}px ${Math.floor((img.width * scale) / 2)}px;
          background-image: url(${dataUri});
          background-repeat: no-repeat;
          background-size: ${img.width * scale}px ${img.height * scale}px;
          color: transparent;
        `
      )
    }
  }
  img.src = url
}

Typescript:

declare global {
  interface Console {
    image: (url: string, backgroundColour?: string, scale?: number) => void
  }
}

/**
 * Display an image in the console.
 */
console.image = function (url: string, backgroundColour: string = 'white', scale: number = 1) {
  const img = new Image()
  img.crossOrigin = "anonymous"
  img.onload = (): void => {
    const c = document.createElement('canvas')
    const ctx = c.getContext('2d')
    if (ctx) {
      c.width = img.width
      c.height = img.height
      ctx.fillStyle = backgroundColour;
      ctx.fillRect(0, 0, c.width, c.height);
      ctx.drawImage(img, 0, 0)
      const dataUri = c.toDataURL('image/png')
      console.log(`%c sup?` ,
        `
          font-size: 1px;
          padding: ${Math.floor((img.height * scale) / 2)}px ${Math.floor((img.width * scale) / 2)}px;
          background-image: url(${dataUri});
          background-repeat: no-repeat;
          background-size: ${img.width * scale}px ${img.height * scale}px;
          color: transparent;
        `
      )
    }
  }
  img.src = url
}
@arcanisgk
Copy link

I tried to use your script to improve my own script, when I try to add icons (png image) to the console.log output I get the image repeated on every line if the content has line breaks, lol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants