html2canvas vs dom-to-image
웹 페이지 콘텐츠 캡처
html2canvasdom-to-image유사 패키지:

웹 페이지 콘텐츠 캡처

웹 페이지 콘텐츠 캡처 라이브러리는 브라우저에서 HTML 요소를 이미지로 변환하는 도구입니다. 이러한 라이브러리는 스크린샷을 찍거나, 그래픽을 생성하거나, 웹 애플리케이션의 특정 부분을 이미지 파일로 저장하는 데 유용합니다. dom-to-image는 DOM 요소를 캡처하여 PNG, JPEG 또는 SVG 형식의 이미지로 변환하는 경량 라이브러리입니다. 이 라이브러리는 CSS 스타일을 포함하여 요소의 시각적 표현을 정확하게 재현합니다. 반면, html2canvas는 HTML 콘텐츠를 캡처하여 캔버스 요소에 그린 다음, 이를 이미지로 변환하는 라이브러리입니다. 이 라이브러리는 페이지의 전체 콘텐츠를 스크린샷처럼 캡처할 수 있으며, 복잡한 레이아웃과 스타일을 처리하는 데 강력합니다.

npm 다운로드 트렌드

3 년

GitHub Stars 순위

통계 세부사항

패키지
다운로드
Stars
크기
Issues
발행일
라이선스
html2canvas27,371,30131,9303.38 MB1,054-MIT
dom-to-image546,05510,767-3379年前MIT

기능 비교: html2canvas vs dom-to-image

이미지 형식 지원

  • html2canvas:

    html2canvas는 캔버스에 이미지를 그린 후, 이를 PNG 형식으로 내보냅니다. JPEG 형식은 지원하지 않지만, 캔버스 데이터를 활용하여 다양한 형식으로 변환할 수 있습니다.

  • dom-to-image:

    dom-to-image는 PNG, JPEG 및 SVG 형식으로 이미지를 생성할 수 있습니다. 특히 SVG 형식 지원이 뛰어나며, 벡터 그래픽을 정확하게 캡처할 수 있습니다.

CSS 스타일 처리

  • html2canvas:

    html2canvas는 요소의 CSS 스타일을 캡처하지만, 일부 복잡한 스타일(예: 애니메이션, 필터)은 완벽하게 재현하지 못할 수 있습니다.

  • dom-to-image:

    dom-to-image는 CSS 스타일을 포함하여 요소의 시각적 표현을 정확하게 재현합니다. 외부 스타일 시트와 인라인 스타일 모두를 지원합니다.

SVG 지원

  • html2canvas:

    html2canvas는 SVG 요소를 캡처할 수 있지만, SVG를 래스터화하여 PNG로 변환합니다. 이 과정에서 벡터 정보가 손실됩니다.

  • dom-to-image:

    dom-to-image는 SVG 요소를 직접 캡처하고, SVG 형식으로 저장할 수 있습니다. SVG 그래픽을 포함한 요소의 이미지를 정확하게 생성합니다.

비동기 처리

  • html2canvas:

    html2canvas는 비동기적으로 캡처 작업을 수행하며, Promise를 반환하여 캡처 완료 후 결과를 처리할 수 있습니다.

  • dom-to-image:

    dom-to-image는 비동기적으로 이미지를 생성하며, Promise를 반환하여 이미지 생성 완료 후 작업을 수행할 수 있습니다.

사용 예시

  • html2canvas:

    html2canvas를 사용한 캡처 예시

    import html2canvas from 'html2canvas';
    const element = document.getElementById('my-element');
    html2canvas(element)
      .then((canvas) => {
        document.body.appendChild(canvas);
        const img = canvas.toDataURL('image/png');
        console.log(img);
      })
      .catch((error) => {
        console.error('Error capturing element:', error);
      });
    
  • dom-to-image:

    dom-to-image를 사용한 이미지 생성 예시

    import { toPng } from 'dom-to-image';
    const node = document.getElementById('my-node');
    toPng(node)
      .then((dataUrl) => {
        const img = new Image();
        img.src = dataUrl;
        document.body.appendChild(img);
      })
      .catch((error) => {
        console.error('Error generating image:', error);
      });
    

선택 방법: html2canvas vs dom-to-image

  • html2canvas:

    html2canvas를 선택하세요. 전체 페이지 또는 특정 요소의 스크린샷을 찍고, 복잡한 레이아웃과 배경 이미지를 포함해야 할 때.

  • dom-to-image:

    dom-to-image를 선택하세요. DOM 요소를 이미지로 변환하고, SVG 지원이 필요하며, CSS 스타일을 정확하게 재현해야 할 때.

html2canvas의 README

html2canvas

Homepage | Downloads | Questions

Gitter CI NPM Downloads NPM Version

JavaScript HTML renderer

The script allows you to take "screenshots" of webpages or parts of it, directly on the users browser. The screenshot is based on the DOM and as such may not be 100% accurate to the real representation as it does not make an actual screenshot, but builds the screenshot based on the information available on the page.

How does it work?

The script renders the current page as a canvas image, by reading the DOM and the different styles applied to the elements.

It does not require any rendering from the server, as the whole image is created on the client's browser. However, as it is heavily dependent on the browser, this library is not suitable to be used in nodejs. It doesn't magically circumvent any browser content policy restrictions either, so rendering cross-origin content will require a proxy to get the content to the same origin.

The script is still in a very experimental state, so I don't recommend using it in a production environment nor start building applications with it yet, as there will be still major changes made.

Browser compatibility

The library should work fine on the following browsers (with Promise polyfill):

  • Firefox 3.5+
  • Google Chrome
  • Opera 12+
  • IE9+
  • Safari 6+

As each CSS property needs to be manually built to be supported, there are a number of properties that are not yet supported.

Usage

The html2canvas library utilizes Promises and expects them to be available in the global context. If you wish to support older browsers that do not natively support Promises, please include a polyfill such as es6-promise before including html2canvas.

To render an element with html2canvas, simply call: html2canvas(element[, options]);

The function returns a Promise containing the <canvas> element. Simply add a promise fulfillment handler to the promise using then:

html2canvas(document.body).then(function(canvas) {
    document.body.appendChild(canvas);
});

Building

You can download ready builds here.

Clone git repository:

$ git clone git://github.com/niklasvh/html2canvas.git

Install dependencies:

$ npm install

Build browser bundle

$ npm run build

Examples

For more information and examples, please visit the homepage or try the test console.

Contributing

If you wish to contribute to the project, please send the pull requests to the develop branch. Before submitting any changes, try and test that the changes work with all the support browsers. If some CSS property isn't supported or is incomplete, please create appropriate tests for it as well before submitting any code changes.