r/javascript • u/pkstn • Sep 02 '15
HTML5 Deck of Cards. 100% vanilla JS.
http://pakastin.github.io/deck-of-cards/12
Sep 02 '15
Just make it so we can flip them over in that interface, and we can play our own games of solitaire.
8
u/pkstn Sep 02 '15 edited Sep 03 '15
Actually the original version had flipping - I will add it :)
3
4
u/rentnil Sep 03 '15
If it just does the card bouncing thing from win95 solitaire we are all set.
3
2
u/pkstn Sep 05 '15 edited Sep 06 '15
Try clicking all the aces once and nothing in between..but don't tell anyone ;D Should use canvas for better performance for this one though..
10
u/Baturinsky Sep 02 '15
Not working in Firefox :(
3
u/pkstn Sep 02 '15
Which version? :/
4
u/yesman_85 Sep 02 '15
The latest.
3
u/pkstn Sep 02 '15
I added Moz -prefix – does it work now? (You might need couple of refreshes..)
4
u/antoninj Sep 02 '15
Nope. :/
2
u/pkstn Sep 02 '15
:( do you get any errors?
3
u/GuruMeditation Sep 02 '15
No errors in the console. The shuffle and sort buttons work; but the cards never move (none of the animations, fan button doesn't work, click drag doesn't work).
3
u/pkstn Sep 02 '15
The problem must with the css prefix - have to check it out..
4
5
1
u/yesman_85 Sep 02 '15
Why don't you try it for yourself? It just doesn't do anything else but show 1 card in the middle of the screen.
3
u/pkstn Sep 02 '15
Works in my Firefox.. Have to check out if I have some older version. I don't use Firefox that much.
4
3
2
u/RenThraysk Sep 02 '15
The code in prefix.js doesn't look right...
https://github.com/pakastin/deck-of-cards/blob/master/lib/prefix.js
In searching for correct prefix, it builds a string... but doesn't test it.
1
u/pkstn Sep 02 '15
Oh crap, you're totally right.. Brain fart 💩
I will fix that right away! Thanks!!
1
u/pkstn Sep 02 '15
Now fixed - thanks again!
1
5
5
u/angellus Sep 02 '15
You have Mixed Content errors on your deck.min.js if you load the page over HTTPS (I have HTTPS everywhere install so it loaded automatically).
You should always use // instead of http:// to prevent this from happening. It will attempt to load the resource over the same protocol as the parent page.
2
2
u/parpaythrums Sep 02 '15
Better yet, use
https://instead!1
u/angellus Sep 02 '15
No argument there, https should be used everywhere.
This can lead to issues also. I have only seen it lead to issues if you use iFrames that have HTTPS when the parent page is not and you are trying to communicate between the two through the use of sockets.
Your best bet is to use // everywhere and than use nginx or some other Web server to force all requests to HTTPS, but this is just getting into the heavy technical side that OP probably does not care about. // gives you the least possible chance of issues because of mixed content which is why I perfer it.
4
u/MrBester Sep 02 '15
Chrome sends
Upgrade-Insecure-Requests: 1in the headers now.Using protocol agnostic URLs is now considered an anti-pattern (plus,
Content-Security-Policywill snarl at you)2
u/pkstn Sep 02 '15 edited Sep 03 '15
I don't want to break things up when used from file:// – if you want to try and tinker locally.. I think I'll keep those https://
2
u/angellus Sep 02 '15
It is definitely not an anti-pattern. Making all the URLs protocol agnostic is a good thing. It makes the application more portable and able to run in more environment (for development or QA). SSL certs are expensive and there is no reason to spend extra to SSL a private network for testing and such. Using external resources from CDNs and such should always be HTTPS if possible.
Also,
Content-Security-Policyonly affects other domains. So using // is a better choice there because self is always allowed.
Upgrade-Insecure-Requests: 1would be completely irrelevant since if the originalGETis upgraded, all of the following requests would HTTPS also.3
u/MrBester Sep 03 '15
I trust Paul Irish: http://www.paulirish.com/2010/the-protocol-relative-url/
Plus there's such a thing as free certificates these days. It's not like when they cost a packet anymore.
1
1
u/myrddin4242 Sep 03 '15
Almost always... Phonegap/Cordova apps wrap a webview which go to file:// 😁
5
u/robotslacker Sep 02 '15
This is cool! Nice work. It'd be pretty cool if you had an API that would allow people to build their own card games off of this.
4
u/pkstn Sep 02 '15 edited Sep 02 '15
Not a bad idea. I could add some new methods to the current API.
Basically now you can do the following:
var deck = Deck()
deck.shuffle()
deck.sort()
deck.bysuit()
deck.fan()
(check out example.js)
That's about it.. But it is pretty easy to fork the project and just add own modules.. But let's see. Might be fun to create a card game engine and see what people would make out of it.
2
u/robotslacker Sep 02 '15
Yeah you might be on to something -- it wouldn't be limited to games with traditional card decks either if the cards were skinnable and you could add different attributes to it, etc.
5
u/pkstn Sep 02 '15
Maybe I should create another more barebones project for that..
2
u/robotslacker Sep 03 '15
I'll give ya my blessing. :)
Can help out too as I've been looking for a side project to work on.
1
u/pkstn Sep 06 '15
I've been gradually refactoring + opening the API. Feel free to fork + pull request if you have something in mind..
4
u/cresquin Sep 02 '15
Looks like a good learning experience.
4
u/pkstn Sep 02 '15 edited Sep 02 '15
Feel free to ask any questions ;)
edit: about the project..
12
u/dhdfdh Sep 02 '15
Why is there air? I mean, why do we need it?
1
u/pkstn Sep 02 '15
What do you mean by air?
1
u/TheIronDev Sep 02 '15
Well you said feel free to ask questions.. He wants to know more about air. Please op.
4
3
u/3Fyr Sep 02 '15
Whatever I do, it always increase z-index, it should be increase/decrease I guess?
Also, some sort of animation could help.
1
u/pkstn Sep 02 '15
I agree, moving is not quite there yet.. Use the buttons on top ;)
3
u/theQuandary Sep 02 '15
I'd also add that the current card should default to the highest z-index (this is how they work in real life and is expected behavior).
1
1
3
Sep 02 '15
I've had this desire to make something absolutely from scratch. Well, as much as possible. Like the endeavour to go out into the woods, find a rock, make shards, get some branches, make an axe, get a tree, get some lumber, etc etc. until I have a bird house or something.
I'm now compelled to do a similar series of projects in JS because of this. I would love to write code to go as far as to define the suits, characters all as vectors, then arrange into cards, then create the deck, etc etc.
I dunno. Rambling. But I think it would be beautiful.
1
2
u/conundrumer Dec 29 '15
I've had this desire to make something absolutely from scratch. Well, as much as possible. Like the endeavour to go out into the woods, find a rock, make shards, get some branches, make an axe, get a tree, get some lumber, etc etc. until I have a bird house or something.
3
u/bart2019 Sep 03 '15
Uh, all cards of the same colour all look like aces, but with a different number, or letter in case of the "people" cards.
1
1
u/pkstn Sep 06 '15
Fixed!
1
2
u/pkstn Sep 03 '15
I just found the 2006 version of Deck of Cards – made with Flash: http://pakastin.fi/cards_2006/
:)
1
u/pkstn Sep 03 '15
Here's first HTML5-version: https://web.archive.org/web/20130823182335/http://pakastin.fi/cards (just close wayback machine controls first..)
2
u/memeship Sep 02 '15
Is there a particular reason why sometimes you're using the notation el.style[prop] and other times el.style.prop?
I haven't seen the first one a whole lot, just curious why you've done it that way, and more so why you're doing it both ways in the same file.
2
u/pkstn Sep 03 '15
Some params need a vendor prefix in some browsers (like transform & transition -> webkitTransform, mozTransform etc..), so that's why I've done it like this:
var transform = prefix('transform')
var transition = prefix('transition')
el.style[transform] = ...
el.style[transition] = ...
Maybe I should name those variables better to avoid the confusion..
1
u/memeship Sep 03 '15
Oh, I see now. You're passing in variables, not strings. That is certainly an interesting approach.
1
1
u/memeship Sep 02 '15
Also curious why you're using dollar signs in front of element variables. Just trying to imitate the look of jQuery or what?
1
0
u/ToucheMonsieur Sep 03 '15
Also curious why you're using dollar signs in front of element variables. Just trying to imitate the look of jQuery or what?
Most likely a style choice.
1
u/memeship Sep 03 '15
Oh I agree on that one. I also prefix stored jQuery objects. But he's not using jQuery, so it doesn't exactly make sense here in that context.
2
u/pkstn Sep 03 '15
I tend to prefix references to DOM elements because I want to separate view objects from DOM elements.. jQuery doesn't own the dollar sign in any way :)
0
u/3Fyr Sep 03 '15
I prefix some functions with dollar sign. I have no idea why.
Have seen others do it too.
3
1
2
u/thecheatah Sep 03 '15
The grabbing algorithm doesn't account for where the card was clicked from, making it twitch and seem unnatural. On click determine the offset and then base moves with the addition of that offset to get a smooth transition.
1
u/pkstn Sep 03 '15 edited Sep 03 '15
Yeah someone else brought that out as well. The problem is that I'm using transform: translate() with percents & pixels mixed, so I can't be certain how much initial offset the card has when dragging starts. I have to fix that one!
1
2
2
u/muffsponge Sep 03 '15
Nice.
But having the cards snap to the middle when dragging is /r/mildlyinfuriating. They should anchor to the point they were clicked. Store an offset when a card is initially pressed.
1
1
1
u/pkstn Sep 08 '15
Added better graphics and a couple of easter eggs..see if you can find them :) https://deck-of-cards.js.org
1
Sep 02 '15
What's it supposed to do? I'm in IE11 (don't judge me--I'm at work) and the buttons don't seem to do anything.
1
u/pkstn Sep 06 '15
I think it's fixed..?
1
Sep 11 '15
All the buttons now work but the suits don't appear. Just numbers. IE11. Windows 7.
Still haven't tried at home. I'll do that and let you know the results as well.
1
u/Tribuadore Sep 02 '15
Wow, I've studied your code, very interesting.
Haven't seen anyone bitch about no semi-colons yet. I used to leave them off too, until I gave up the fight and just started conforming with everyone else. I really like your style.
Something I've never seen before either is for example your queue.js, where given an object it assigns .queue to the object and returns. Took me a minute to work out what was going on. I'm looking at the code thinking "this function doesn't do anything", then I realised the queue function is in scope even though it's defined after the return.
I'm wondering could you also put the queuing variable after the return too?
4
u/randfur Sep 02 '15
That's JavaScript's function hoisting where function definitions are implicitly moved to the top of their scope. Variables get hoisted to the top of their scope as well with the value undefined, it isn't until the point in the code that you specify that they get assigned something else. This means you can use variables before you've assigned them and they'll have the value undefined.
2
u/pkstn Sep 02 '15
Yes, that's true. In this case it's not possible to define queueing after return:
return target
var queueing = []
Queueing would get hoisted, but the value would be undefined - I think. Function definitions work a little differently. Although some old IE might crash and burn, but minification (uglifyjs) should fix the order, I think..
4
u/MrBester Sep 02 '15 edited Sep 02 '15
queueingwould get hoisted, but the value would be undefined - I think.Correct.
While you know what you're doing, newer developers may not. Hence I advocate declaring
vars (uninitialised until you need them), internalfunctions, then the meat of the main function withreturns liberally spread around.Anything to minimise the slightest possibility that old IE will cough. It already has enough ways, so why give it more?
You also get to see at a glance just how much you're declaring - instead of picking through the code - so you can tell if you really ought to refactor it...
1
1
u/pkstn Sep 02 '15
Yeah, I tend to keep the "basic structure" of the function as high as possible (initializations and returns).. Hoisting does the magic.
About those semicolons: I use standard with sublime and it's been awesome!
1
u/pkstn Sep 03 '15
..and the produced output have semicolons: https://github.com/pakastin/deck-of-cards/blob/master/dist/deck.js
;)
1
u/I_Pork_Saucy_Ladies Sep 03 '15
As a proponent of semicolons, thank you for choosing the pragmatic approach. :)
1
u/nightlily Sep 02 '15
Looks good!
I would recommend changing the way card stacking works so it is more intuitive/consistent. At the moment, when I move a card around it is hard to tell whether the card is going to go under or above the card I stack it with.
Try playing a game of solitaire and you'll see what I mean.
1
u/pkstn Sep 03 '15
I agree. The moving around bit is not quite there yet.. I made a stupid choice with using only translate but not saving the current values. I will fix that.
1
1
u/anlumo Sep 02 '15
Please add a license to the repository!
1
u/pkstn Sep 03 '15
Thanks! I have no idea about open source licenses. Which one would you recommend? There is "MIT" text in package.json, so maybe I copypaste that one from somewhere..?
1
u/anlumo Sep 03 '15
MIT would be good, yeah.
1
1
u/pkstn Sep 06 '15
Sorry, I had to change to GPL, since the new graphics are also licensed GPL.. Hope that want cause any problem. 0.0.x versions are still MIT, you can find them from the "releases"-tab.
1
u/anlumo Sep 06 '15
You can still license your part MIT, because this license is GPL-compatible. Just the whole thing including the graphics would be GPL then.
1
u/pkstn Sep 07 '15
The problem is I modified the graphics. When theming is possible, I could separate graphics and change the license back, but like I said, I want to respect Chris having choosed GPL for now.
1
1
1
1
1
1
u/BeniBin Sep 03 '15
I would definitely read a blog post about the process of making this.
2
u/pkstn Sep 03 '15 edited Sep 03 '15
Actually the story goes back to (at least) 2006: http://pakastin.fi/cards_2006/ (made with Flash)
..then I made Angular.js version, which I can't find anywhere – but it was on Chrome Experiments back then.
And now I made vanilla JS version..
I got the original idea because vending machines with card games had premade animation back then which I found unrealistic. I though if it would be hard to animate better. Turned out it wasn't :)
3
u/pkstn Sep 03 '15 edited Sep 05 '15
I just found the Angular.js version as well: https://web.archive.org/web/20130823182335/http://pakastin.fi/cards
Close wayback machine's topbar to use the controls ;)
2
u/pkstn Sep 06 '15
I got inspired by your idea - planning to write. I've never wrote a blog, but you got to start somewhere :)
1
u/BeniBin Sep 07 '15
Awesome ! Thanks for doing this :)
1
u/pkstn Sep 07 '15
I was thinking that because I'm not a native English speaker, that would be a great learning experience as well. Especially if readers could correct my grammar, it would be a win-win! :)
1
1
Sep 03 '15
The z-index of a card seems to be incremented on each click by 1. I think the desired behavior sorts the currently clicked card to be on top of the others, so maybe it should be set to max + 1 on a click.
1
u/pkstn Sep 03 '15
Yeah, I should fix that. You're not the first to tell me about this, but thanks anyway :D
1
1
u/pkstn Sep 06 '15
Should work now!
1
Sep 09 '15
Nice
1
u/pkstn Sep 09 '15
Btw. try click all kings in a row ;)
1
Sep 10 '15
Nice! For a moment, I thought it was going to do the solitaire winning animation, but easter eggs are always fun.
1
1
Dec 01 '15
I was checking if you did a fair shuffle, and found this
rnd = Math.random() * i | 0I'm impressed that you can OR 0 to round.
1
u/pkstn Dec 02 '15
OR is double pipes (||), one pipe (|) turns number to integer..
1
Dec 02 '15
One pipe is a bitwise OR. You don't typically do OR operations with floating point, so javascript rounds down to an integer first.
1
u/pkstn Dec 02 '15
You're correct, sorry I thought you were talking about the logical operator. But yeah, bitwise OR is useful, because it floors down the number - looks so much cleaner IMO. I also use bitwise NOT to check if indexOf is over -1:
~haystack.indexOf('needle')instead ofhaystack.indexOf('needle') > -1..
0
Sep 02 '15
[deleted]
1
u/pkstn Sep 03 '15
It's actually a feature, but I know that it is not that intuitive. Have to fix the whole moving around logic..
0
37
u/papers_ Sep 02 '15
Huge up vote for using ES6 here. Good job man!