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

DOMの画像化について #2

Open
Daiki-Shiraki opened this issue Oct 28, 2023 · 0 comments
Open

DOMの画像化について #2

Daiki-Shiraki opened this issue Oct 28, 2023 · 0 comments
Assignees

Comments

@Daiki-Shiraki
Copy link
Collaborator

Daiki-Shiraki commented Oct 28, 2023

DOMの画像化はおそらく、html2canvasがいいんじゃなかろうかと思った。
過去に画像化を使用した際のコードがあるから参考にして下さい。

import React, { useEffect } from 'react'
import { Button, Container } from 'react-bootstrap' // Button追加
import html2canvas from 'html2canvas'

type Props = {
    name:string
}
const MakePicture: React.FC<Props> = ( {name}) => { 
    const makePicture = async () => {
      const elem = document.querySelector("#target") as HTMLElement;  
      console.log(elem)
    html2canvas(elem, {scale:2,allowTaint:true}).then(function(canvas) {
      const a = document.createElement('a')
      const dataURI = canvas.toDataURL("image/png");
      a.href = dataURI
      const name = name +'.png'
      a.download = name
      a.click()
    });    
      }
return (
<>
  <Button className='mt-3' type='button' variant='primary' onClick={makePicture}>保存する</Button> {/* 追記 */}
</>
)
}
export default MakePicture
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