()
| 1123 | |
| 1124 | /* Start drawing on a canvas */ |
| 1125 | var start = function start() { |
| 1126 | // For dimensions, clearCanvas etc., |
| 1127 | // we only care about the first element. |
| 1128 | var canvas = elements[0]; |
| 1129 | |
| 1130 | if (canvas.getContext) { |
| 1131 | ngx = Math.ceil(canvas.width / g); |
| 1132 | ngy = Math.ceil(canvas.height / g); |
| 1133 | } else { |
| 1134 | var rect = canvas.getBoundingClientRect(); |
| 1135 | ngx = Math.ceil(rect.width / g); |
| 1136 | ngy = Math.ceil(rect.height / g); |
| 1137 | } |
| 1138 | |
| 1139 | // Sending a wordcloudstart event which cause the previous loop to stop. |
| 1140 | // Do nothing if the event is canceled. |
| 1141 | if (!sendEvent('wordcloudstart', true)) { |
| 1142 | return; |
| 1143 | } |
| 1144 | |
| 1145 | // Determine the center of the word cloud |
| 1146 | center = settings.origin |
| 1147 | ? [settings.origin[0] / g, settings.origin[1] / g] |
| 1148 | : [ngx / 2, ngy / 2]; |
| 1149 | |
| 1150 | // Maxium radius to look for space |
| 1151 | maxRadius = Math.floor(Math.sqrt(ngx * ngx + ngy * ngy)); |
| 1152 | |
| 1153 | /* Clear the canvas only if the clearCanvas is set, |
| 1154 | if not, update the grid to the current canvas state */ |
| 1155 | grid = []; |
| 1156 | |
| 1157 | var gx, gy, i; |
| 1158 | if (!canvas.getContext || settings.clearCanvas) { |
| 1159 | elements.forEach(function (el) { |
| 1160 | if (el.getContext) { |
| 1161 | var ctx = el.getContext('2d'); |
| 1162 | ctx.fillStyle = settings.backgroundColor; |
| 1163 | ctx.clearRect(0, 0, ngx * (g + 1), ngy * (g + 1)); |
| 1164 | ctx.fillRect(0, 0, ngx * (g + 1), ngy * (g + 1)); |
| 1165 | } else { |
| 1166 | el.textContent = ''; |
| 1167 | el.style.backgroundColor = settings.backgroundColor; |
| 1168 | el.style.position = 'relative'; |
| 1169 | } |
| 1170 | }); |
| 1171 | |
| 1172 | /* fill the grid with empty state */ |
| 1173 | gx = ngx; |
| 1174 | while (gx--) { |
| 1175 | grid[gx] = []; |
| 1176 | gy = ngy; |
| 1177 | while (gy--) { |
| 1178 | grid[gx][gy] = true; |
| 1179 | } |
| 1180 | } |
| 1181 | } else { |
| 1182 | /* Determine bgPixel by creating |
no test coverage detected