Skip to content

Instantly share code, notes, and snippets.

View AlexKardone's full-sized avatar

Alexander Matskevich AlexKardone

View GitHub Profile
return obj !== null && typeof obj === 'object' && !Array.isArray(obj);
function isObject(obj) {
return obj?.constructor.name === 'Object';
}
@AlexKardone
AlexKardone / Calculate the angle of shooting
Created August 26, 2019 14:14
Calculate the angle of shooting
shapes.point.x = shapes.point.x + shapes.point.velocity * Math.cos(angle / 180 * Math.PI);
shapes.point.y = shapes.point.y + shapes.point.velocity * Math.sin(angle / 180 * Math.PI);
@AlexKardone
AlexKardone / Check overlap between two rectangles
Last active July 31, 2019 17:21
Check overlap between two rectangles
var checkCollisions = function(rect1, rect2) {
var xOverlap = Math.abs(rect1.x - rect2.x) <= Math.max(rect1.width, rect2.width);
var yOverlap = Math.abs(rect1.y - rect2.y) <= Math.max(rect1.height, rect2.height);
return xOverlap && yOverlap;
}
@AlexKardone
AlexKardone / Phone field mask
Created May 24, 2019 13:49
Phone field mask
<script>
$(document).ready(function() {
$("#phone").mask("+7 (999) 99-99-999", {autoclear: false});
$("#phone").click(function(){
if($("#phone").val()=="+7 (___) __-__-___") {
$('#phone')[0].selectionStart = 4;
$('#phone')[0].selectionEnd = 4;
}
})
});
@AlexKardone
AlexKardone / Get the document height
Created May 23, 2019 08:39
Get the document width/height
var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera') > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);
function getDocumentHeight() {
return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}
function getViewportHeight() {
return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
@AlexKardone
AlexKardone / Drag&Drop an element in the page
Created May 7, 2019 05:27
Drag&Drop an element in the page
'use strict';
var setup = document.querySelector('.setup');
var dialogHandler = setup.querySelector('.setup-user input');
dialogHandler.addEventListener('mousedown', function(evt) {
evt.preventDefault();
var startCoords = {
x: evt.clientX,
y: evt.clientY
'use strict';
var ESC_KEYCODE = 27;
var ENTER_KEYCODE = 13;
// Нажатие на элемент .setup-open удаляет класс hidden
// у блока setup. Нажатие на элемент .setup-close, расположенный
// внутри блока setup возвращает ему класс hidden.
var setup = document.querySelector('.setup');
var setupOpen = document.querySelector('.setup-open');
@AlexKardone
AlexKardone / Close popup when click outside it
Created April 28, 2019 07:47
Close popup when click outside it
// get the container of the popup window
var wind = setup.querySelector('.popup-window');
var closePopup = function() {
setup.classList.add('hidden');
};
document.body.addEventListener('click', function(evt) {
if (!wind.contains(evt.target)) {
closePopup();
@AlexKardone
AlexKardone / gist:fe7d6bea46863538a68b0b8690beaeb8
Created December 8, 2018 20:04
Hide element after tap outside an element (for Iphone)
$('html').on('touchstart', function(e) {
$('.navbar-flyout').hide();
})
$(".navbar-flyout").on('touchstart',function(e) {
e.stopPropagation();
});
// http://qaru.site/questions/1412280/javascript-jquery-tap-outside-an-element-on-an-iphone