-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathImage.vue
65 lines (58 loc) · 2.32 KB
/
Image.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<template>
<Page>
<ActionBar title="Image Demo" />
<StackLayout>
<CanvasView ref="canvas" width="100%" height="100%" @draw="onDraw" @touch="onTouch" />
</StackLayout>
</Page>
</template>
<script lang="ts">
import { BitmapShader, CanvasView, createRect, Matrix, Paint, Style, TileMode } from '@nativescript-community/ui-canvas';
import { Frame, TouchGestureEventData, ImageSource, knownFolders, path } from '@nativescript/core';
import Vue, {NativeScriptVue} from 'nativescript-vue';
import { Component } from 'vue-property-decorator';
const iconLocalFile: ImageSource = ImageSource.fromFileSync(path.join(knownFolders.currentApp().path, 'assets/images/test.jpg'));
const shaderPaint = new Paint()
shaderPaint.style= Style.FILL
const cirlcePaint = new Paint()
cirlcePaint.strokeWidth = 4;
cirlcePaint.style= Style.STROKE
cirlcePaint.color= 'red'
@Component
export default class Image extends Vue {
onBack() {
Frame.topmost().goBack();
}
touchX = 0
touchY = 0
onTouch(event: TouchGestureEventData) {
this.touchX = event.getX();
this.touchY = event.getY();
(this.$refs['canvas'] as NativeScriptVue<CanvasView>).nativeView.invalidate()
}
onDraw(event: { canvas }) {
try {
const canvas = event.canvas;
canvas.drawBitmap(iconLocalFile, null, createRect(0, 0, canvas.getWidth(), canvas.getHeight()), null);
// const deviceScale = screen.mainScreen.scale;
// canvas.scale(deviceScale, deviceScale); // always scale to device density to work with dp
const ratioX = canvas.getWidth() / iconLocalFile.width
const ratioY = canvas.getHeight() / iconLocalFile.height
const matrix = new Matrix()
matrix.postScale(ratioX, ratioY, 0,0)
matrix.postScale(
2,
2,
this.touchX,this.touchY
)
const shader = new BitmapShader(iconLocalFile, TileMode.CLAMP, TileMode.CLAMP);
shader.setLocalMatrix(matrix)
shaderPaint.setShader(shader)
canvas.drawCircle(this.touchX, this.touchY, 60, shaderPaint)
canvas.drawCircle(this.touchX, this.touchY, 60, cirlcePaint)
} catch (error) {
console.error(error, error.stack);
}
}
}
</script>