so://phie

outline

A web-based drawing app
git clone git://soophie.de/outline

blob: bdd146cefa0492ac9a65af5578f4995f0f8f59d1 (4001 bytes)[doc][blame][raw]

1const DEVICE_PIXEL_RATIO = window.devicePixelRatio || 1;

2

3import { COLORS } from '../constant.js';

4

5let active;

6let point_list = [];

7let width;

8let height;

9let size;

10let ratio;

11let backing_store_ratio;

12

13let canvas;

14let overlay;

15let memory;

16let g;

17let o;

18let m;

19

20function init() {

21 width = window.innerWidth;

22 height = window.innerHeight;

23 canvas = document.querySelector('#canvas');

24 overlay = document.querySelector('#overlay');

25 memory = document.createElement('canvas');

26 g = canvas.getContext('2d');

27 o = overlay.getContext('2d');

28 m = memory.getContext('2d');

29 resize();

30 window.addEventListener('resize', resize);

31}

32

33function get_canvas() {

34 return canvas;

35}

36

37function resize() {

38 width = window.innerWidth;

39 height = window.innerHeight;

40 let max = width > height ? width : height;

41 if(!size || max > size) {

42 size = max;

43 }

44 backing_store_ratio = (

45 g.webkitBackingStorePixelRatio ||

46 g.mozBackingStorePixelRatio ||

47 g.msBackingStorePixelRatio ||

48 g.oBackingStorePixelRatio ||

49 g.backingStorePixelRatio || 1

50 )

51 ratio = DEVICE_PIXEL_RATIO / backing_store_ratio;

52 let image_data;

53 image_data = g.getImageData(0, 0, size * ratio, size * ratio);

54 scale_canvas(canvas, g);

55 g.putImageData(image_data, 0, 0);

56 g.imageSmoothingEnabled = false;

57 image_data = o.getImageData(0, 0, size * ratio, size * ratio);

58 scale_canvas(overlay, o);

59 o.putImageData(image_data, 0, 0);

60 o.imageSmoothingEnabled = false;

61 image_data = m.getImageData(0, 0, size * ratio, size * ratio);

62 scale_canvas(memory, m);

63 m.putImageData(image_data, 0, 0);

64 m.imageSmoothingEnabled = false;

65 o.clearRect(0, 0, window.innerWidth, window.innerHeight);

66}

67

68function scale_canvas(canvas, g) {

69 if(DEVICE_PIXEL_RATIO !== backing_store_ratio) {

70 canvas.width = size * ratio;

71 canvas.height = size * ratio;

72 canvas.style.width = size + 'px';

73 canvas.style.height = size + 'px';

74 }

75 else {

76 canvas.width = size;

77 canvas.height = size;

78 canvas.style.width = '';

79 canvas.style.height = '';

80 }

81 g.scale(ratio, ratio);

82}

83

84function clear() {

85 g.clearRect(0, 0, size, size);

86 m.clearRect(0, 0, size, size);

87}

88

89function on_press(event, state) {

90 point_list.push({

91 x: event.layerX,

92 y: event.layerY

93 });

94 active = true;

95}

96

97function on_move(event, state) {

98 if(active) {

99 g.clearRect(0, 0, size, size);

100 g.drawImage(memory, 0, 0, size, size);

101 point_list.push({

102 x: event.layerX,

103 y: event.layerY

104 });

105 draw_curve(state);

106 }

107}

108

109function on_release(event) {

110 if(active) {

111 if(event && event.type === 'pointerup') {

112 active = false;

113 }

114 m.clearRect(0, 0, size, size);

115 m.drawImage(canvas, 0, 0, size, size);

116 point_list = [];

117 }

118}

119

120function draw_curve(state) {

121 g.lineJoin = 'round';

122 g.lineCap = 'round';

123 g.lineWidth = 2 * state.radius;

124 g.strokeStyle = COLORS[state.color] + '';

125 if(point_list.length < 3) {

126 g.fillStyle = COLORS[state.color] + '';

127 g.beginPath();

128 g.arc(point_list[0].x, point_list[0].y, g.lineWidth / 2, 0, Math.PI * 2, !0);

129 g.closePath();

130 g.fill();

131 return;

132 }

133 g.beginPath();

134 g.moveTo(point_list[0].x, point_list[0].y);

135 let i;

136 for(i = 1; i < point_list.length - 2; i++) {

137 let dx = (point_list[i].x + point_list[i + 1].x);

138 let dy = (point_list[i].y + point_list[i + 1].y);

139 g.quadraticCurveTo(point_list[i].x, point_list[i].y, parseInt(dx / 2), parseInt(dy / 2));

140 }

141 g.quadraticCurveTo(point_list[i].x, point_list[i].y, point_list[i + 1].x, point_list[i + 1].y);

142 g.stroke();

143}

144

145function draw_cursor(state) {

146 o.clearRect(0, 0, window.innerWidth, window.innerHeight);

147 if(state.point) {

148 if(!active) {

149 o.fillStyle = COLORS[state.color] + '';

150 o.beginPath();

151 o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);

152 o.closePath();

153 o.fill();

154 }

155 o.strokeStyle = 'white';

156 o.beginPath();

157 o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);

158 o.closePath();

159 o.stroke();

160 }

161}

162

163export default {

164 init,

165 get_canvas,

166 resize,

167 scale_canvas,

168 clear,

169 on_press,

170 on_move,

171 on_release,

172 draw_curve,

173 draw_cursor

174};