-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo6.html
94 lines (86 loc) · 2.95 KB
/
demo6.html
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/3.0.9/pixi.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script>
<title>demo</title>
</head>
<body style="position:absolute;">
</body>
</html>
<script type="text/javascript">
$(document).ready(function() {
var renderer = PIXI.autoDetectRenderer(1000,1000,{backgroundColor : 0xEEEEEE});
$(renderer.view).attr('style', 'position:absolute; z-index:1;');
$("body").append(renderer.view);
var stage = new PIXI.Container();
//set a background
var background_sprite = new PIXI.Sprite();
background_sprite.height = renderer.height;
background_sprite.width = renderer.width;
var empty_backround = new PIXI.Graphics();
empty_backround.beginFill(0xEEEEEE, 1);
empty_backround.moveTo(0, 0);
empty_backround.lineTo(renderer.width, 0);
empty_backround.lineTo(renderer.width, renderer.height);
empty_backround.lineTo(0, renderer.height);
empty_backround.lineTo(0, 0);
empty_backround.endFill();
background_sprite.texture = empty_backround.generateTexture();
stage.addChild(empty_backround);
renderer.render(stage);
var _context, _canvas;
var last_draw_time;
var last_x, last_y;
function start(e) {
var mouse_location = e.data.getLocalPosition(stage);
_canvas = document.createElement("canvas");
$(_canvas).attr('style', 'position:absolute; z-index:2; pointer-events:none');
$(_canvas).attr('id', 'draw_canvas');
_canvas.width = renderer.width;
_canvas.height = renderer.height;
_context = _canvas.getContext("2d");
_context.lineWidth = 4;
_context.strokeStyle = '#000000';
_context.lineCap = "round";
_context.beginPath();
_context.moveTo(mouse_location.x, mouse_location.y);
last_x = mouse_location.x;
last_y = mouse_location.y;
$(renderer.view).parent().append(_canvas);
last_draw_time = 0;
stage.mousemove = move;
stage.mouseup = end;
}
function move(e) {
//limit updates to once ever 10ms
var time = Date.now();
var time_diff = time - last_draw_time;
if (time_diff < 10) return;
last_draw_time = time;
//only move 0.3 in the direction of the pointer, this smooths it out
var mouse_location = e.data.getLocalPosition(stage);
var new_x = last_x + 0.3 * (mouse_location.x - last_x);
var new_y = last_y + 0.3 * (mouse_location.y - last_y);
last_x = new_x;
last_y = new_y;
_context.lineTo(new_x, new_y);
_context.stroke();
}
function end(e) {
var mouse_location = e.data.getLocalPosition(stage);
_context.lineTo(mouse_location.x, mouse_location.y);
_context.stroke();
var foregroundTexture = PIXI.Texture.fromCanvas(_canvas);
var foregroundSprite = new PIXI.Sprite(foregroundTexture);
stage.addChild(foregroundSprite);
$("#draw_canvas").remove()
renderer.render(stage);
delete stage.mousemove;
delete stage.mouseup;
}
stage.interactive = true;
stage.mousedown = start;
});
</script>