最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using svg.js with TypeScript - Stack Overflow

programmeradmin1浏览0评论

I'm making a website app using TypeScript, and I want to use the svg.js library.

I'm coding in TypeScript, and automatically piling into JavaScript which runs in the browser.

Is this possible? And if so, how?

I'm making a website app using TypeScript, and I want to use the svg.js library.

I'm coding in TypeScript, and automatically piling into JavaScript which runs in the browser.

Is this possible? And if so, how?

Share Improve this question asked Feb 12, 2018 at 1:01 MagmaticMagmatic 1,9493 gold badges21 silver badges41 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

svg.js provides a .d.ts file so you can use it in typescript out of box.

I am using vscode and it needs no further configuration to provide intellisense.

CommonJS style

ES 6 style or

vscode integrates js and ts service tightly, so it may provide some kind of auto detection. I never used Atom but I think you can explicitly declare your reference using Triple-Slash Directives

/// <reference path="path/to/yourTypeDeclaration.d.ts" />
// your code follows

If importing this still not helps, you may lack some kind of language service plugins.

Quite late to the party but since I have wasted most of the day struggling to use svg.js from typescript I want to share the solution:

First (of course) you need to install the svg.js via npm.

npm install svg.js --save

Installed package contains the so called typedef file svg.js.d.ts.

You will also need to setup webpack or your favorite packager in order to prepare package for the web.

In your '.ts' file you paste:

import * as SVG from 'svg.js';

function a() {
    let draw = SVG(window.document.body).size('100%', '100%')

    draw.rect(400, 400).fill({ color: '#f00', opacity: 1 })
}


window.onload = (event) => {
    a()
};

This will render a red rectangle.

发布评论

评论列表(0)

  1. 暂无评论