-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathperspective.js
179 lines (166 loc) · 5.19 KB
/
perspective.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* -------------------------------------------------------------------
* define objects (name space) for this library.
* ----------------------------------------------------------------- */
if( typeof html5jp == 'undefined' ) {
html5jp = new Object();
}
(function () {
/* -------------------------------------------------------------------
* constructor
* ----------------------------------------------------------------- */
html5jp.perspective = function(ctxd, image) {
// check the arguments
if( ! ctxd || ! ctxd.strokeStyle ) { return; }
if( ! image || ! image.width || ! image.height ) { return; }
// prepare a <canvas> for the image
var cvso = document.createElement('canvas');
cvso.width = parseInt(image.width);
cvso.height = parseInt(image.height);
var ctxo = cvso.getContext('2d');
ctxo.drawImage(image, 0, 0, cvso.width, cvso.height);
// prepare a <canvas> for the transformed image
var cvst = document.createElement('canvas');
cvst.width = ctxd.canvas.width;
cvst.height = ctxd.canvas.height;
var ctxt = cvst.getContext('2d');
// parameters
this.p = {
ctxd: ctxd,
cvso: cvso,
ctxo: ctxo,
ctxt: ctxt
}
};
/* -------------------------------------------------------------------
* prototypes
* ----------------------------------------------------------------- */
var proto = html5jp.perspective.prototype;
/* -------------------------------------------------------------------
* public methods
* ----------------------------------------------------------------- */
proto.draw = function(points) {
var d0x = points[0][0];
var d0y = points[0][1];
var d1x = points[1][0];
var d1y = points[1][1];
var d2x = points[2][0];
var d2y = points[2][1];
var d3x = points[3][0];
var d3y = points[3][1];
// compute the dimension of each side
var dims = [
Math.sqrt( Math.pow(d0x-d1x, 2) + Math.pow(d0y-d1y, 2) ), // top side
Math.sqrt( Math.pow(d1x-d2x, 2) + Math.pow(d1y-d2y, 2) ), // right side
Math.sqrt( Math.pow(d2x-d3x, 2) + Math.pow(d2y-d3y, 2) ), // bottom side
Math.sqrt( Math.pow(d3x-d0x, 2) + Math.pow(d3y-d0y, 2) ) // left side
];
//
var ow = this.p.cvso.width;
var oh = this.p.cvso.height;
// specify the index of which dimension is longest
var base_index = 0;
var max_scale_rate = 0;
var zero_num = 0;
for( var i=0; i<4; i++ ) {
var rate = 0;
if( i % 2 ) {
rate = dims[i] / ow;
} else {
rate = dims[i] / oh;
}
if( rate > max_scale_rate ) {
base_index = i;
max_scale_rate = rate;
}
if( dims[i] == 0 ) {
zero_num ++;
}
}
if(zero_num > 1) { return; }
//
var step = 2;
var cover_step = step * 5;
//
var ctxo = this.p.ctxo;
var ctxt = this.p.ctxt;
ctxt.clearRect(0, 0, ctxt.canvas.width, ctxt.canvas.height);
if(base_index % 2 == 0) { // top or bottom side
var ctxl = this.create_canvas_context(ow, cover_step);
var cvsl = ctxl.canvas;
for( var y=0; y<oh; y+=step ) {
var r = y / oh;
var sx = d0x + (d3x-d0x) * r;
var sy = d0y + (d3y-d0y) * r;
var ex = d1x + (d2x-d1x) * r;
var ey = d1y + (d2y-d1y) * r;
var ag = Math.atan( (ey-sy) / (ex-sx) );
var sc = Math.sqrt( Math.pow(ex-sx, 2) + Math.pow(ey-sy, 2) ) / ow;
ctxl.setTransform(1, 0, 0, 1, 0, -y);
ctxl.drawImage(ctxo.canvas, 0, 0);
//
ctxt.translate(sx, sy);
ctxt.rotate(ag);
ctxt.scale(sc, sc);
ctxt.drawImage(cvsl, 0, 0);
//
ctxt.setTransform(1, 0, 0, 1, 0, 0);
}
} else if(base_index % 2 == 1) { // right or left side
var ctxl = this.create_canvas_context(cover_step, oh);
var cvsl = ctxl.canvas;
for( var x=0; x<ow; x+=step ) {
var r = x / ow;
var sx = d0x + (d1x-d0x) * r;
var sy = d0y + (d1y-d0y) * r;
var ex = d3x + (d2x-d3x) * r;
var ey = d3y + (d2y-d3y) * r;
var ag = Math.atan( (sx-ex) / (ey-sy) );
var sc = Math.sqrt( Math.pow(ex-sx, 2) + Math.pow(ey-sy, 2) ) / oh;
ctxl.setTransform(1, 0, 0, 1, -x, 0);
ctxl.drawImage(ctxo.canvas, 0, 0);
//
ctxt.translate(sx, sy);
ctxt.rotate(ag);
ctxt.scale(sc, sc);
ctxt.drawImage(cvsl, 0, 0);
//
ctxt.setTransform(1, 0, 0, 1, 0, 0);
}
}
// set a clipping path and draw the transformed image on the destination canvas.
this.p.ctxd.save();
this.set_clipping_path(this.p.ctxd, [[d0x, d0y], [d1x, d1y], [d2x, d2y], [d3x, d3y]]);
this.p.ctxd.drawImage(ctxt.canvas, 0, 0);
this.p.ctxd.restore();
}
/* -------------------------------------------------------------------
* private methods
* ----------------------------------------------------------------- */
proto.create_canvas_context = function(w, h) {
var canvas = document.createElement('canvas');
canvas.width = w;
canvas.height = h;
var ctx = canvas.getContext('2d');
return ctx;
};
proto.set_clipping_path = function(ctx, points) {
ctx.beginPath();
ctx.moveTo(points[0][0], points[0][1]);
for( var i=1; i<points.length; i++ ) {
ctx.lineTo(points[i][0], points[i][1]);
}
ctx.closePath();
ctx.clip();
};
public function listarArquivosRemessa()
{
$arrayRemessa = array_map('pathinfo', \File::files('assets/remessa'));
$arrayView = array_pluck($arrayRemessa, 'dirname');
// (^) Isso vai extrair apenas as keys que você precisa
return view ("PesquisaView")->with("arrayRemessa", $arrayView);
}
//E na view pode ficar assim:
@foreach($arrayRemessa as $item)
<option>{{ $item }}</option>
@endfor
})();