Konva学习笔记0-创建项目

Konva学习笔记0-创建项目

安装webpack

npm i webpack webpack-cli -g

新建项目文件夹

mkdir demo
cd demo

安装辅助框架

webpack-cli init

此时会提醒是否安装xx包,输入y+enter即可开始安装
在这里插入图片描述

再次初始化项目

在这里插入图片描述

安装完毕会报错,不理它,再次初始化

webpack-cli init

设置项目选项

脚手架提供了一系列初始化选择,提供博主个人的选择,凭自主喜好即可
在这里插入图片描述K

安装并配置Konva

npm i konva

编辑tsconfig.json,在compilerOptions节点中添加lib和moduleResolution

{
    "compilerOptions": {
        "allowSyntheticDefaultImports": true,
        "noImplicitAny": true,
        "module": "es6",
        "target": "es5",
        "allowJs": true,
        "lib": [ "es6", "dom" ],
        "moduleResolution": "node"
    }
}

测试Demo

修改index.html

<!DOCTYPE html>
<html>
 <head>
     <meta charset="utf-8" />
     <title>Webpack App</title>
 </head>
 <body>
     <div id="container"></div>
 </body>
</html>

修改src/index.ts

import Konva from "konva";

const width = window.innerWidth;
const height = window.innerHeight;

const stage = new Konva.Stage({
   container: 'container',
   width: width,
   height: height
});

const layer = new Konva.Layer();

const rect1 = new Konva.Rect({
   x: 20,
   y: 20,
   width: 100,
   height: 50,
   fill: 'green',
   stroke: 'black',
   strokeWidth: 4
});
// add the shape to the layer
layer.add(rect1);

const rect2 = new Konva.Rect({
   x: 150,
   y: 40,
   width: 100,
   height: 50,
   fill: 'red',
   shadowBlur: 10,
   cornerRadius: 10
});
layer.add(rect2);

const rect3 = new Konva.Rect({
   x: 50,
   y: 120,
   width: 100,
   height: 100,
   fill: 'blue',
   cornerRadius: [0, 10, 20, 30]
});
layer.add(rect3);

// add the layer to the stage
stage.add(layer);

最后运行

npm run serve

可以在浏览器输入地址 http://localhost:8080/ 看到最终结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxhls0208

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值