In my previous post, I talked about how to draw lines with different widths and colors on HTML5 canvas elements, very easy!
In this post, We will discuss about how to record the user movements and "play" it on the canvas.
The main idea is to save each mouse position to an array, loop over the array and paint on the canvas element.
The Array definition in Javascript:
var recordDataArray = []; // best practice to use [] instead of new Array();
On each mousedown/mousemove event we need to push to the array the following data:
- mouse x position
- mouse y position
- event type (mousedown to begin a new line path or mousemove to continue the line path on the canvas)
- line color
- line width
Define the event type enum in Javascript:
var DrawEventType = {
START: 0,
MOVE: 1
}
Now, We will modify our functions from the previous post to push the data to the array:
var startPaintWithMouse = function(event) {
mouseState = 1;
brushCtx = board.getContext('2d');
brushCtx.beginPath();
brushCtx.lineWidth = lineWidth;
brushCtx.strokeStyle = color;
brushCtx.moveTo(event.offsetX,event.offsetY);
// push to array
recordDataArray.push({
type: DrawEventType.START,
x:event.offsetX,
y:event.offsetY,
color:color,
width:lineWidth
});
};
var movePaintWithMouse = function(event) {
if (mouseState == 1) {
event.preventDefault();
brushCtx = board.getContext('2d');
brushCtx.lineTo(event.offsetX,event.offsetY);
brushCtx.stroke();
recordDataArray.push({ type:DrawEventType.MOVE,
x:event.offsetX,
y:event.offsetY,
color:color,
width:lineWidth
});
}
};
We can create a button that clear the canvas.
Clearing the canvas in Javascript is easy too:
var brushCtx = board.getContext('2d');
brushCtx.clearRect(0, 0, board.width, board.height);
And now, we can create a play button that loop over the array and paint the user movements on the canvas.
We need to use the setInterval javascript method to give the user the drawing experience.
var playDataArray = function(dataArray,speed) {
var dataIndex = 0;
var playInterval = setInterval(function(){
if (typeof(dataArray[dataIndex]) == 'object') {
drawDataObject(dataArray[dataIndex]);
dataIndex++;
} else {
clearInterval(playInterval);
}
},speed);
};
On each interval iteration we call the drawDataObject method which gets an object from our array and draw it.
var drawDataObject = function(data) {
if (data.type == DrawEventType.START) {
brushCtx = board.getContext('2d');
brushCtx.beginPath();
brushCtx.lineWidth = data.width;
brushCtx.strokeStyle = data.color;
brushCtx.moveTo(data.x,data.y);
} else if (data.type == DrawEventType.MOVE) {
brushCtx = board.getContext('2d');
brushCtx.lineTo(data.x,data.y);
brushCtx.stroke();
}
};
That's all!
I'll try to make it this way. Thank you for your tips. I hope that they will be useful for me.
ReplyDeleteGreat Article
DeleteIEEE Final Year Projects for CSE
IEEE Project Centers in Chennai
Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from Javascript Training in Chennai . or learn thru JavaScript Online Training India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai
ReplyDeleteThank you for sharing this post.Really very informative post you shared here. Kindly keep blogging.
ReplyDeleteBest Software testing Training in Bangalore
Best web Designing Training in Bangalore
I suppose that we need to use this method to give the user the best drawing experience possible.
ReplyDeleteIf you wish for to improve your know-how only keep visiting this site and be updated with the most up-to-date news update posted here. yahoo email login
ReplyDeleteAWS Training in Bangalore - Live Online & Classroom
ReplyDeletemyTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.
IOT Training in Bangalore - Live Online & Classroom
IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.
just information we only provide information for those who need it
ReplyDeleteA. cara agar cepat hamil setelah selesai haid
B. cara agar cepat hamil
C. cara alami untuk segera mendapat kehamilan
D. makanan dan minuman agar cepat hamil
E. masa subur biar cepat hamil
F. panduan agar cepat hamil
The article is very exciting to read. By the way, your dog is truly a best friend. Happy many returns of the day! pikview
ReplyDeleteAwesome post
ReplyDeleteiot courses in Bangalore
internet of things training course in Bangalore
internet of things course in Bangalore
Nice information
ReplyDeletereliaable resdenza
This is the motivation behind why it is the most well known application store for Android. On the off chance that you will ApkMarkt
ReplyDeletethe application, it would be an extraordinary plan to chec
descargar lucky patcher apk
ReplyDeleteIt's Really A Great Post. Looking For Some More Stuff.
ReplyDeleteshriram break free
That's a beautiful post. I can't wait to utilize the resources you've shared with us. Do share more such informative posts.
ReplyDeleteUnix Training in Chennai
Unix Shell Scripting Training in Chennai
JavaScript Training in Chennai
JavaScript Course in Chennai
C C++ Training in Chennai
C Training in Chennai
Unix Training in Tambaram
Unix Training in Adyar
Your article inspired me more and keep writing.
ReplyDeleteSoftware testing training in chennai
JAVA Training in Chennai
Hadoop Training in Chennai
Selenium Training in Chennai
web designing Training in chennai
German Classes in chennai
cloud computing training in chennai
Cloud Computing Training in Porur
ReplyDeletevery nice post thankyou for sharing.
Sobha Royal Pavilion Sarjapur Road
The post was really good. Thanks for sharing
ReplyDeleteshriram earth plots
ReplyDeleteAmazing Article ! I would like to thank you for the efforts you had made for writing this awesome article.
Thanks for sharing such a nice info.I hope you will share more information like this. please keep on sharing!
internship in chennai
internship in chennai for cse
internship for mba in chennai
internship in chennai for hr
internship in chennai for mba
companies for internship in chennai
internship in chennai for ece
paid internship in chennai
internship in chennai for biotechnology
internship in chennai for b.com students
Amazing Article,Really useful information to all So, I hope you will share more information to be check and share here.
ReplyDeleteinplant training
inplant training chennai
inplant training meaning
inplant training certificate
inplant training report
report for inplant training
inplant training certificate format
inplant training meaning in tamil
what is inplant training
inplant training in chennai for mechanical
Thanks for sharing such a great blog......!!!
ReplyDeletefull form
full form of nrc
nrc full form
mbbs full form
full form of rip
I got more useful information from this thanks for share this blog
ReplyDeletepurpose of cloud computing
importance of cloud computing
benefits of machine learning
benefits of devops
php vs .net
javascript questions and answers
ethical hacking interview questions and answers pdf