aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/InkingStroke.tsx
blob: d724421d3213e88423c85c4ad119e90dc0f1f051 (plain)
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
66
import { observer } from "mobx-react";
import { observable } from "mobx";
import { InkingControl } from "./InkingControl";
import { InkTool } from "../../fields/InkField";
import React = require("react");


interface StrokeProps {
    id: string;
    line: Array<{ x: number, y: number }>;
    color: string;
    width: string;
    tool: InkTool;
    deleteCallback: (index: string) => void;
}

@observer
export class InkingStroke extends React.Component<StrokeProps> {

    @observable private _strokeTool: InkTool = this.props.tool;
    @observable private _strokeColor: string = this.props.color;
    @observable private _strokeWidth: string = this.props.width;

    private _canvasColor: string = "#cdcdcd";

    deleteStroke = (e: React.MouseEvent): void => {
        if (InkingControl.Instance.selectedTool === InkTool.Eraser && e.buttons === 1) {
            this.props.deleteCallback(this.props.id);
        }
    }

    parseData = (line: Array<{ x: number, y: number }>): string => {
        if (line.length === 0) {
            return "";
        }
        const pathData = "M " +
            line.map(p => {
                return p.x + " " + p.y;
            }).join(" L ");
        return pathData;
    }

    createStyle() {
        switch (this._strokeTool) {
            // add more tool styles here
            default:
                return {
                    fill: "none",
                    stroke: this._strokeColor,
                    strokeWidth: this._strokeWidth + "px",
                }
        }
    }


    render() {
        let pathStyle = this.createStyle();
        let pathData = this.parseData(this.props.line);

        return (
            <path className={(this._strokeTool === InkTool.Highlighter) ? "highlight" : ""}
                d={pathData} style={pathStyle} strokeLinejoin="round" strokeLinecap="round"
                onMouseOver={this.deleteStroke} onMouseDown={this.deleteStroke} />
        )
    }
}