目录
Paper.js的使用 安装Paper 引入Paper.js 创建画布 实例化Paper、Project以及Tool 画圆 画点和线 画矩形 导入图片 画文字 Item组 曲线 监听键盘事件 监听鼠标事件 设置动画 下载成图片 完整代码
Paper.js的使用
安装Paper
npm install paper
引入Paper.js
import * as Paper from "paper" ;
创建画布
< template>
< canvas id= "myCanvas" ref= "myCanvas" > < / canvas>
< / template>
实例化Paper、Project以及Tool
const paperInstance = new Paper. PaperScope ( ) ;
let project : null | paper. Project = null ;
const tool = new Paper. Tool ( ) ;
onMounted ( ( ) => {
paperInstance. setup ( document. getElementById ( "myCanvas" ) as HTMLCanvasElement) ;
project = new Paper. Project (
document. getElementById ( "myCanvas" ) as HTMLCanvasElement
) ;
tool. activate ( ) ;
} ) ;
画圆
const drawCircle = ( ) => {
const circle = new Paper. Path. Circle ( {
center : [ 200 , 100 ] ,
radius : 50 ,
fillColor : new Paper. Color ( "red" ) ,
strokeColor : new Paper. Color ( "black" ) ,
} ) ;
circle. strokeWidth = 2 ;
} ;
画点和线
const drawPointAndLine = ( ) => {
const point = new Paper. Point ( 500 , 100 ) ;
const path = new Paper. Path ( ) ;
path. add ( point) ;
const copyPoint = point. clone ( ) ;
copyPoint. set ( 600 , 100 ) ;
path. add ( copyPoint) ;
path. strokeColor = new Paper. Color ( "black" ) ;
path. strokeWidth = 4 ;
path. selected = true ;
} ;
画矩形
const drawRectangle = ( ) => {
const rect1 = new Paper. Path. Rectangle ( {
center : [ 200 , 400 ] ,
size : [ 50 , 100 ] ,
radius : [ 5 , 5 ] ,
fillColor : "orange" ,
strokeColor : "black" ,
strokeWidth : 4 ,
rotation : 45 ,
selected : true ,
} ) ;
const topLeft = new Paper. Point ( 200 , 20 ) ;
const rectSize = new Paper. Size ( 200 , 200 ) ;
const rect2 = new Paper. Rectangle ( topLeft, rectSize) ;
const radius = new Paper. Size ( 30 , 30 ) ;
const path = new Paper. Path. Rectangle ( rect2, radius) ;
path. fillColor = new Paper. Color ( "blue" ) ;
path. position. set ( {
x : 300 ,
y : 300 ,
} ) ;
path. selected = true ;
} ;
导入图片
import Demo1 from "../../assets/demo1.jpg" ;
const drawImg = ( ) => {
const img = new Paper. Raster ( Demo1) ;
img. position. set ( 700 , 600 ) ;
img. scale ( 0.2 ) ;
} ;
画文字
const drawText = ( ) => {
const text = new Paper. PointText ( {
position : [ 500 , 200 ] ,
fillColor : "black" ,
justification : "center" ,
fontSize : 20 ,
content : "测试文字" ,
locked : false ,
} ) ;
} ;
Item组
const drawGroup = ( ) => {
const group = new Paper. Group ( ) ;
const text = new Paper. PointText ( {
position : [ 0 , - 50 - 10 ] ,
content : "圆" ,
fillColor : "black" ,
justification : "center" ,
fontSize : 20 ,
} ) ;
const circle = new Paper. Path. Circle ( {
center : [ 0 , 0 ] ,
radius : 50 ,
fillColor : "red" ,
} ) ;
group. addChildren ( [ text, circle] ) ;
group. position. set ( 1200 , 800 ) ;
} ;
曲线
const drawCurve = ( ) => {
const group = new Paper. Group ( ) ;
const earth = new Paper. Path. Circle ( {
center : [ 800 , 200 ] ,
radius : 50 ,
fillColor : new Paper. Color ( "green" ) ,
} ) ;
var firstPoint1 = calculatePointOnCircle ( 800 , 200 , 50 , ( Math. PI / 18 ) * 19 ) ;
var secondPoint1 = calculatePointOnCircle ( 800 , 200 , 50 , ( Math. PI / 18 ) * 1 ) ;
var handleOut1 = new Paper. Point ( 160 , 60 ) ;
var handleIn1 = new Paper. Point ( - 160 , 20 ) ;
var firstSegment = new Paper. Segment ( firstPoint1, undefined , handleIn1) ;
var secondSegment = new Paper. Segment ( secondPoint1, handleOut1, undefined ) ;
const curve1 = new Paper. Path ( [ firstSegment, secondSegment] ) ;
curve1. strokeColor = new Paper. Color ( "black" ) ;
curve1. strokeWidth = 8 ;
curve1. locked = true ;
curve1. selected = false ;
group. addChildren ( [ earth, curve1] ) ;
} ;
监听键盘事件
const onKeyDown = ( ) => {
tool. onKeyDown = ( event : any) => {
console. log ( event) ;
if ( event. event. key === "z" && event. event. ctrlKey) {
}
} ;
} ;
监听鼠标事件
const onMouse = ( ) => {
const children = project?. activeLayer. children;
tool. onMouseDown = ( event : any) => {
if ( ! event. item) {
children?. forEach ( ( item : any) => {
item. selected = false ;
} ) ;
} else {
children?. forEach ( ( item : any) => {
item. selected = false ;
} ) ;
event. item. selected = true ;
}
} ;
children?. forEach ( ( item : any) => {
item. onMouseDown = ( ) => {
project?. activeLayer. addChild ( item) ;
item. selected = true ;
} ;
item. onMouseDrag = ( event : paper. MouseEvent) => {
item. position. set (
item. position. x + event. delta. x,
item. position. y + event. delta. y
) ;
} ;
} ) ;
} ;
设置动画
let direction = "right" ;
const drawAnimate = ( ) => {
const node = new Paper. Path. Circle ( {
center : [ 200 , 100 ] ,
radius : 50 ,
} ) ;
node. strokeColor = new Paper. Color ( "black" ) ;
node. fillColor = new Paper. Color ( "white" ) ;
node. onFrame = ( ) => {
const maxWidth = document. body. clientWidth;
if ( direction === "right" ) {
if ( node. bounds. right >= maxWidth) {
direction = "left" ;
}
} else {
if ( node. bounds. left <= 0 ) {
direction = "right" ;
}
}
if ( direction === "right" ) {
node. bounds. x += 5 ;
} else {
node. bounds. x -= 5 ;
}
} ;
} ;
下载成图片
const screenShot = ( ) => {
const flash = project?. exportSVG ( ) ;
var serializer = new XMLSerializer ( ) ;
var xmlString = serializer. serializeToString ( flash as SVGAElement) ;
var blob = new Blob ( [ xmlString] , { type : "image/svg+xml;charset=utf-8" } ) ;
var url = URL . createObjectURL ( blob) ;
var a = document. createElement ( "a" ) ;
a. href = url;
a. download = "my-svg-file.svg" ;
document. body. appendChild ( a) ;
a. click ( ) ;
document. body. removeChild ( a) ;
} ;
完整代码
< template>
< canvas id= "myCanvas" ref= "myCanvas" > < / canvas>
< / template>
< script lang= "ts" >
export default { name : "Paper" } ;
< / script>
< script lang= "ts" setup>
import * as Paper from "paper" ;
import { onMounted } from "vue" ;
import Demo1 from "../../assets/demo1.jpg" ;
const paperInstance = new Paper. PaperScope ( ) ;
let project : null | paper. Project = null ;
const tool = new Paper. Tool ( ) ;
let direction = "right" ;
const drawAnimate = ( ) => {
const node = new Paper. Path. Circle ( {
center : [ 200 , 100 ] ,
radius : 50 ,
} ) ;
node. strokeColor = new Paper. Color ( "black" ) ;
node. fillColor = new Paper. Color ( "white" ) ;
node. onFrame = ( ) => {
const maxWidth = document. body. clientWidth;
if ( direction === "right" ) {
if ( node. bounds. right >= maxWidth) {
direction = "left" ;
}
} else {
if ( node. bounds. left <= 0 ) {
direction = "right" ;
}
}
if ( direction === "right" ) {
node. bounds. x += 5 ;
} else {
node. bounds. x -= 5 ;
}
} ;
} ;
const drawCircle = ( ) => {
const circle = new Paper. Path. Circle ( {
center : [ 200 , 100 ] ,
radius : 50 ,
fillColor : new Paper. Color ( "red" ) ,
strokeColor : new Paper. Color ( "black" ) ,
} ) ;
circle. strokeWidth = 2 ;
const pathData = "M 50 100 L 100 0 L 0 0 Z" ;
const path = new Paper. Path ( pathData) ;
path. strokeColor = new Paper. Color ( "black" ) ;
path. fillColor = new Paper. Color ( "green" ) ;
path. strokeWidth = 2 ;
path. strokeWidth = 2 ;
path. position. set ( 400 , 100 ) ;
} ;
const drawPointAndLine = ( ) => {
const point = new Paper. Point ( 500 , 100 ) ;
const path = new Paper. Path ( ) ;
path. add ( point) ;
const copyPoint = point. clone ( ) ;
copyPoint. set ( 600 , 100 ) ;
path. add ( copyPoint) ;
path. strokeColor = new Paper. Color ( "black" ) ;
path. strokeWidth = 4 ;
path. selected = true ;
} ;
const drawRectangle = ( ) => {
const rect1 = new Paper. Path. Rectangle ( {
center : [ 200 , 400 ] ,
size : [ 50 , 100 ] ,
radius : [ 5 , 5 ] ,
fillColor : "orange" ,
strokeColor : "black" ,
strokeWidth : 4 ,
rotation : 45 ,
selected : true ,
} ) ;
const topLeft = new Paper. Point ( 200 , 20 ) ;
const rectSize = new Paper. Size ( 200 , 200 ) ;
const rect2 = new Paper. Rectangle ( topLeft, rectSize) ;
const radius = new Paper. Size ( 30 , 30 ) ;
const path = new Paper. Path. Rectangle ( rect2, radius) ;
path. fillColor = new Paper. Color ( "blue" ) ;
path. position. set ( {
x : 300 ,
y : 300 ,
} ) ;
path. selected = true ;
} ;
const drawImg = ( ) => {
const img = new Paper. Raster ( Demo1) ;
img. position. set ( 700 , 600 ) ;
img. scale ( 0.2 ) ;
} ;
const drawText = ( ) => {
const text = new Paper. PointText ( {
position : [ 1600 , 50 ] ,
fillColor : "black" ,
justification : "center" ,
fontSize : 20 ,
content : "下载" ,
locked : false ,
name : "download" ,
} ) ;
} ;
const drawGroup = ( ) => {
const group = new Paper. Group ( ) ;
const text = new Paper. PointText ( {
position : [ 0 , - 50 - 10 ] ,
content : "圆" ,
fillColor : "black" ,
justification : "center" ,
fontSize : 20 ,
} ) ;
const circle = new Paper. Path. Circle ( {
center : [ 0 , 0 ] ,
radius : 50 ,
fillColor : "red" ,
} ) ;
group. addChildren ( [ text, circle] ) ;
group. position. set ( 1200 , 800 ) ;
} ;
const drawCurve = ( ) => {
const group = new Paper. Group ( ) ;
const earth = new Paper. Path. Circle ( {
center : [ 800 , 200 ] ,
radius : 50 ,
fillColor : new Paper. Color ( "green" ) ,
} ) ;
var firstPoint1 = calculatePointOnCircle ( 800 , 200 , 50 , ( Math. PI / 18 ) * 19 ) ;
var secondPoint1 = calculatePointOnCircle ( 800 , 200 , 50 , ( Math. PI / 18 ) * 1 ) ;
var handleOut1 = new Paper. Point ( 160 , 60 ) ;
var handleIn1 = new Paper. Point ( - 160 , 20 ) ;
var firstSegment = new Paper. Segment ( firstPoint1, undefined , handleIn1) ;
var secondSegment = new Paper. Segment ( secondPoint1, handleOut1, undefined ) ;
const curve1 = new Paper. Path ( [ firstSegment, secondSegment] ) ;
curve1. strokeColor = new Paper. Color ( "black" ) ;
curve1. strokeWidth = 8 ;
curve1. locked = true ;
curve1. selected = false ;
group. addChildren ( [ earth, curve1] ) ;
} ;
const onKeyDown = ( ) => {
tool. onKeyDown = ( event : any) => {
console. log ( event) ;
if ( event. event. key === "z" && event. event. ctrlKey) {
}
} ;
} ;
const onMouse = ( ) => {
const children = project?. activeLayer. children;
tool. onMouseDown = ( event : any) => {
if ( ! event. item) {
children?. forEach ( ( item : any) => {
item. selected = false ;
} ) ;
} else {
children?. forEach ( ( item : any) => {
item. selected = false ;
} ) ;
event. item. selected = true ;
}
const download = project?. getItem ( {
name : "download" ,
} ) ;
if ( download === event. item) {
screenShot ( ) ;
}
} ;
console. log ( children) ;
children?. forEach ( ( item : any) => {
item. onMouseDown = ( ) => {
project?. activeLayer. addChild ( item) ;
item. selected = true ;
} ;
item. onMouseDrag = ( event : paper. MouseEvent) => {
console. log ( event) ;
item. position. set (
item. position. x + event. delta. x,
item. position. y + event. delta. y
) ;
} ;
} ) ;
} ;
const screenShot = ( ) => {
const flash = project?. exportSVG ( ) ;
var serializer = new XMLSerializer ( ) ;
var xmlString = serializer. serializeToString ( flash as SVGAElement) ;
var blob = new Blob ( [ xmlString] , { type : "image/svg+xml;charset=utf-8" } ) ;
var url = URL . createObjectURL ( blob) ;
var a = document. createElement ( "a" ) ;
a. href = url;
a. download = "my-svg-file.svg" ;
document. body. appendChild ( a) ;
a. click ( ) ;
document. body. removeChild ( a) ;
} ;
const calculatePointOnCircle = (
centerX : number,
centerY : number,
radius : number,
angle : number
) => {
var x = centerX + radius * Math. cos ( angle) ;
var y = centerY + radius * Math. sin ( angle) ;
console. log ( x, y) ;
return new Paper. Point ( x, y) ;
} ;
onMounted ( ( ) => {
paperInstance. setup ( document. getElementById ( "myCanvas" ) as HTMLCanvasElement) ;
project = new Paper. Project (
document. getElementById ( "myCanvas" ) as HTMLCanvasElement
) ;
drawCircle ( ) ;
drawPointAndLine ( ) ;
drawRectangle ( ) ;
drawImg ( ) ;
drawText ( ) ;
drawGroup ( ) ;
drawCurve ( ) ;
tool. activate ( ) ;
onKeyDown ( ) ;
onMouse ( ) ;
drawAnimate ( ) ;
} ) ;
< / script>
< style lang= "scss" scoped>
#myCanvas {
width : 100 % ;
height : 100 % ;
}
< / style>